<?php

namespace app\admin\controller;

use app\admin\service\InterfereExportService;
use app\admin\service\InterfereImportService;
use app\admin\service\ReportService;
use app\admin\service\UserService;
use app\BaseController;
use app\common\service\ExportService;
use app\model\ConfigModel;
use app\model\ConsultOfflineModel;
use app\model\ConsultOfflineResultModel;
use app\model\ConsultOfflineTaskModel;
use app\model\ConsultOnlineMessageModel;
use app\model\ConsultOnlineModel;
use app\model\ConsultOnlineResultModel;
use app\model\CrisisBehaviorModel;
use app\model\CrisisLevelModel;
use app\model\CrisisLogModel;
use app\model\CrisisMeasuresModel;
use app\model\CrisisTypeModel;
use app\model\CrisisUserModel;
use app\model\DepartmentAdminModel;
use app\model\DepartmentModel;
use app\model\FollowUserModel;
use app\model\group_activity\GroupActivityTaskUserResultModel;
use app\model\InterfereAdminModel;
use app\model\InterfereBehaviorModel;
use app\model\InterfereReportModel;
use app\model\InterfereUserModel;
use app\model\MessageModel;
use app\model\ProxyEvaluationResultModel;
use app\model\RoleModel;
use app\model\SelfEvaluationResultModel;
use app\model\TaskUserResultModel;
use app\model\training\TrainingTaskUserResultModel;
use app\model\UnitModel;
use app\model\UserModel;
use app\model\not_del\UserModel as NotDelUserModel;
use app\model\wireless\WirelessEvaluationResultModel;
use OSS\Credentials\StaticCredentialsProvider;
use OSS\OssClient;
use think\App;
use think\facade\Filesystem;
use ZipArchive;


class Interfere extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 判断权限
     * @return bool
     */
    public function isAuth(): bool
    {
        if ($this->userData['role_id'] > 0) {
            $UnitModel = new UnitModel();
            $interfere_admin_status = $UnitModel->where('id', '=', $this->userData['unit_id'])->value('interfere_admin_status');
            if ($interfere_admin_status == 1) {
                $InterfereAdminModel = new InterfereAdminModel();
                $uid = $InterfereAdminModel->where('unit_id', '=', $this->userData['unit_id'])->distinct(true)->column('uid');
                if (in_array($this->userData['id'], $uid)) {
                    return true;
                }
            }
            return false;
        }
        return true;
    }

    /**
     * 申请人姓名选择器
     * @return bool|string
     */
    public function OperatorNameSelector(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereUserModel = new InterfereUserModel();
            $uids = $InterfereUserModel->where('unit_id', '=', $this->userData['unit_id'])->distinct(true)->column('operator_uid');
            $UserModel = new NotDelUserModel();
            if ($id) {
                $data = $UserModel->where([
                    ['id', 'in', $uids],
                    ['role_id', '=', $id]
                ])->field('id,role_id,name')->select()->toArray();
            } else {
                $user = $UserModel->where('id', 'in', $uids)->field('id,role_id,name')->select()->toArray();
                $role_id = array_column($user, 'role_id');
                $role_id = array_unique($role_id);
                $RoleModel = new RoleModel();
                $data = $RoleModel->where('id', 'in', $role_id)->field('id,name')->select()->toArray();
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预措施选择器
     * @return bool|string
     */
    public function CrisisMeasuresSelector(): bool|string
    {
        try {
            $where = [];
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $where[] = ['status', '=', 1];
            $CrisisMeasuresModel = new CrisisMeasuresModel();
            $data = $CrisisMeasuresModel->where($where)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预优先级配置
     * @return bool|string
     */
    public function InterferePriorityConfig(): bool|string
    {
        $data = [
            ['id' => 1, 'name' => '高优先度'],
            ['id' => 2, 'name' => '中优先度'],
            ['id' => 3, 'name' => '低优先度'],
        ];
        return success($data);
    }

    /**
     * 关注等级配置
     * @return bool|string
     */
    public function FollowLevelConfig(): bool|string
    {
        $data = [
            ['id' => 1, 'name' => '轻度'],
            ['id' => 2, 'name' => '中度'],
            ['id' => 3, 'name' => '重度'],
            ['id' => 4, 'name' => '密切']
        ];
        return success($data);
    }

    /**
     * 关注渠道配置
     * @return bool|string
     */
    public function FollowChannelConfig(): bool|string
    {
        $data = [
            ['id' => 1, 'name' => '危机上报手动移入'],
            ['id' => 2, 'name' => '任务测评自动加入'],
            ['id' => 3, 'name' => '自我测评自动加入'],
            ['id' => 4, 'name' => '他人代测自动移入'],
            ['id' => 5, 'name' => '无线测评自动移入'],
            ['id' => 6, 'name' => '线上咨询手动移入'],
            ['id' => 7, 'name' => '线下预约手动移入'],
            ['id' => 8, 'name' => '关注人群手动导入'],
//            ['id' => 9, 'name' => '干预人群手动导入'],
            ['id' => 10, 'name' => '智慧果手动加入'],
            ['id' => 11, 'name' => '团体活动手动加入'],
            ['id' => 12, 'name' => '培训讲座手动加入'],
            ['id' => 13, 'name' => '心理评估手动加入'],
        ];
        return success($data);
    }

    /**
     * 干预渠道配置
     * @return bool|string
     */
    public function InterfereChannelConfig(): bool|string
    {
        $data = [
            ['id' => 1, 'name' => '危机上报手动移入'],
            ['id' => 2, 'name' => '任务测评自动加入'],
            ['id' => 3, 'name' => '自我测评自动加入'],
            ['id' => 4, 'name' => '他人代测自动移入'],
            ['id' => 5, 'name' => '无线测评自动移入'],
            ['id' => 6, 'name' => '线上咨询手动移入'],
            ['id' => 7, 'name' => '线下预约手动移入'],
            ['id' => 8, 'name' => '关注人群手动导入'],
            ['id' => 9, 'name' => '干预人群手动导入'],
            ['id' => 10, 'name' => '智慧果手动加入'],
            ['id' => 11, 'name' => '团体活动手动加入'],
            ['id' => 12, 'name' => '培训讲座手动加入'],
            ['id' => 13, 'name' => '心理评估手动加入'],
        ];
        return success($data);
    }

    /**
     * 干预人群-搜索栏
     * @return bool|string
     */
    public function InterfereSearch(): bool|string
    {
        $param = $this->request->param();
        if (!isset($param['status'])) {
            $where[] = ['status', '=', 0];
        } else {
            $where[] = ['status', '=', 1];
        }
        $InterfereUserModel = new InterfereUserModel();
        $UserModel = new NotDelUserModel();
        $where[] = ['unit_id', '=', $this->userData['unit_id']];
        $uids = $InterfereUserModel->where($where)->column('uid');
        $department_id = $UserModel->where('id', 'in', $uids)->column('department_id');
        $department_id = array_unique($department_id);
        $DepartmentModel = new DepartmentModel();
        $data['department'] = $DepartmentModel->where('id', 'in', $department_id)->select()->toArray();
        $UserService = new UserService($this->app);
        $DepartmentAffiliationAll = $UserService->getNotDelDepartmentAffiliationAll($this->userData['unit_id']);
        foreach ($data['department'] as &$item) {
            $item['name'] = $DepartmentAffiliationAll[$item['id']]['all_name'] ?? '--';
        }
        return success($data);
    }

    /**
     * 关注人群-搜索栏
     * @return bool|string
     */
    public function FollowSearch(): bool|string
    {
        $FollowUserModel = new FollowUserModel();
        $data['up_num'] = $FollowUserModel->where('unit_id', '=', $this->userData['unit_id'])->order('up_num desc')->value('up_num') ?? 0;
        $operator_uid = $FollowUserModel->where('unit_id', '=', $this->userData['unit_id'])->column('operator_uid');
        $operator_uid = array_unique($operator_uid);
        $UserModel = new NotDelUserModel();
        $data['operator'] = $UserModel->where('id', 'in', $operator_uid)->field('id,name')->select()->toArray();
        $uids = $FollowUserModel->where('unit_id', '=', $this->userData['unit_id'])->column('uid');
        $department_id = $UserModel->where('id', 'in', $uids)->column('department_id');
        $department_id = array_unique($department_id);
        $DepartmentModel = new DepartmentModel();
        $data['department'] = $DepartmentModel->where('id', 'in', $department_id)->select()->toArray();
        $UserService = new UserService($this->app);
        $DepartmentAffiliationAll = $UserService->getDepartmentAffiliationAll($this->userData['unit_id']);
        foreach ($data['department'] as &$v) {
            $v['name'] = $DepartmentAffiliationAll[$v['id']]['all_name'] ?? '--';
        }
        return success($data);
    }

    /**
     * 危机问题类型列表
     * @return bool|string
     */
    public function CrisisTypeList(): bool|string
    {
        try {
            $param = $this->request->param();
            $CrisisTypeModel = new CrisisTypeModel();
            $query = $CrisisTypeModel->where('unit_id', '=', $this->userData['unit_id']);
            $data = $query->with([
                'user' => function ($query) {
                    $query->with(['roleName']);
                }
            ])->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$item) {
                if (empty($item['role_name'])) {
                    $item['role_name'] = '超级管理员';
                }
            }
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机问题类型添加
     * @return bool|string
     */
    public function CrisisTypeAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'name|类型名称' => 'require',
                'explain|类型说明' => 'require'
            ]);
            $CrisisTypeModel = new CrisisTypeModel();
            if ($CrisisTypeModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['name', '=', $param['name']]
            ])->value('id')) {
                throw new \Exception('不可重名！');
            }
            $param['unit_id'] = $this->userData['unit_id'];
            $param['uid'] = $this->userData['id'];
            $param['time'] = time();
            $CrisisTypeModel->insert($param);
            return success([], '添加成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机问题类型详情
     * @return bool|string
     */
    public function CrisisTypeInfo(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisTypeModel = new CrisisTypeModel();
            $data = $CrisisTypeModel->find($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机问题类型编辑
     * @return bool|string
     */
    public function CrisisTypeUpdate(): bool|string
    {
        try {
            $param = $this->request->param();
            $CrisisTypeModel = new CrisisTypeModel();
            $info = $CrisisTypeModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if (!empty($param['name']) && $param['name'] != $info['name']) {
                if ($CrisisTypeModel->where([
                    ['unit_id', '=', $this->userData['unit_id']],
                    ['name', '=', $param['name']]
                ])->value('id')) {
                    throw new \Exception('不可重名！');
                }
            }
            $info->save($param);
            return success([], '修改成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机问题类型删除
     * @return bool|string
     */
    public function CrisisTypeDel(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisTypeModel = new CrisisTypeModel();
            if (is_array($id)) {
                $name = $CrisisTypeModel->where('id', 'in', $id)->column('name');
                if (in_array('无异常表现', $name)) {
                    throw new \Exception('【无异常表现】类别不可删除！');
                }
                $CrisisTypeModel->where('id', 'in', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            } else {
                $info = $CrisisTypeModel->find($id);
                if (!$info) throw new \Exception('参数错误！');
                $info->save([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }
            return success([], '删除成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机问题类型修改启用状态
     * @return bool|string
     */
    public function CrisisTypeStatus(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $status = $this->request->param('status');
            $CrisisTypeModel = new CrisisTypeModel();
            if (is_array($id)) {
                $CrisisTypeModel->where('id', 'in', $id)->update([
                    'status' => $status
                ]);
            } else {
                $CrisisTypeModel->where('id', '=', $id)->update([
                    'status' => $status
                ]);
            }
            return success([], '修改成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预措施列表
     * @return bool|string
     */
    public function CrisisMeasuresList(): bool|string
    {
        try {
            $param = $this->request->param();
            $CrisisMeasuresModel = new CrisisMeasuresModel();
            $query = $CrisisMeasuresModel->where('unit_id', '=', $this->userData['unit_id']);
            $data = $query->with([
                'user' => function ($query) {
                    $query->with(['roleName']);
                }
            ])->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$item) {
                if (empty($item['role_name'])) {
                    $item['role_name'] = '超级管理员';
                }
            }
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 干预措施添加
     * @return bool|string
     */
    public function CrisisMeasuresAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'name|名称' => 'require',
            ]);
            $CrisisMeasuresModel = new CrisisMeasuresModel();
            if ($CrisisMeasuresModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['name', '=', $param['name']]
            ])->value('id')) {
                throw new \Exception('不可重名！');
            }
            $param['unit_id'] = $this->userData['unit_id'];
            $param['uid'] = $this->userData['id'];
            $param['time'] = time();
            $CrisisMeasuresModel->insert($param);
            return success([], '添加成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预措施详情
     * @return bool|string
     */
    public function CrisisMeasuresInfo(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisMeasuresModel = new CrisisMeasuresModel();
            $data = $CrisisMeasuresModel->find($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预措施编辑
     * @return bool|string
     */
    public function CrisisMeasuresUpdate(): bool|string
    {
        try {
            $param = $this->request->param();
            $CrisisMeasuresModel = new CrisisMeasuresModel();
            $info = $CrisisMeasuresModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if (!empty($param['name']) && $param['name'] != $info['name']) {
                if ($CrisisMeasuresModel->where([
                    ['unit_id', '=', $this->userData['unit_id']],
                    ['name', '=', $param['name']]
                ])->value('id')) {
                    throw new \Exception('不可重名！');
                }
            }
            $info->save($param);
            return success([], '修改成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预措施删除
     * @return bool|string
     */
    public function CrisisMeasuresDel(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisMeasuresModel = new CrisisMeasuresModel();
            if (is_array($id)) {
                $CrisisMeasuresModel->where('id', 'in', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            } else {
                $CrisisMeasuresModel->where('id', '=', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }
            return success([], '删除成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预措施修改启用状态
     * @return bool|string
     */
    public function CrisisMeasuresStatus(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $status = $this->request->param('status');
            $CrisisMeasuresModel = new CrisisMeasuresModel();
            if (is_array($id)) {
                $CrisisMeasuresModel->where('id', 'in', $id)->update([
                    'status' => $status
                ]);
            } else {
                $CrisisMeasuresModel->where('id', '=', $id)->update([
                    'status' => $status
                ]);
            }
            return success([], '修改成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 评估风险等级列表
     * @return bool|string
     */
    public function CrisisLevelList(): bool|string
    {
        try {
            $param = $this->request->param();
            $CrisisLevelModel = new CrisisLevelModel();
            $query = $CrisisLevelModel->where('unit_id', '=', $this->userData['unit_id']);
            $data = $query->with([
                'user' => function ($query) {
                    $query->with(['roleName']);
                }
            ])->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$item) {
                if (empty($item['role_name'])) {
                    $item['role_name'] = '超级管理员';
                }
            }
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 评估风险等级添加
     * @return bool|string
     */
    public function CrisisLevelAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'name|名称' => 'require',
                'explain|危险程度表现' => 'require'
            ]);
            $CrisisLevelModel = new CrisisLevelModel();
            if ($CrisisLevelModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['name', '=', $param['name']]
            ])->value('id')) {
                throw new \Exception('不可重名！');
            }
            $param['unit_id'] = $this->userData['unit_id'];
            $param['uid'] = $this->userData['id'];
            $param['time'] = time();
            $CrisisLevelModel->insert($param);
            return success([], '添加成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 评估风险等级详情
     * @return bool|string
     */
    public function CrisisLevelInfo(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisLevelModel = new CrisisLevelModel();
            $data = $CrisisLevelModel->find($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 评估风险等级编辑
     * @return bool|string
     */
    public function CrisisLevelUpdate(): bool|string
    {
        try {
            $param = $this->request->param();
            $CrisisLevelModel = new CrisisLevelModel();
            $info = $CrisisLevelModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if (!empty($param['name']) && $param['name'] != $info['name']) {
                if ($CrisisLevelModel->where([
                    ['unit_id', '=', $this->userData['unit_id']],
                    ['name', '=', $param['name']]
                ])->value('id')) {
                    throw new \Exception('不可重名！');
                }
            }
            $info->save($param);
            return success([], '修改成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 评估风险等级删除
     * @return bool|string
     */
    public function CrisisLevelDel(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisLevelModel = new CrisisLevelModel();
            if (is_array($id)) {
                $CrisisLevelModel->where('id', 'in', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            } else {
                $CrisisLevelModel->where('id', '=', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }
            return success([], '删除成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 评估风险等级修改启用状态
     * @return bool|string
     */
    public function CrisisLevelStatus(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $status = $this->request->param('status');
            $CrisisLevelModel = new CrisisLevelModel();
            if (is_array($id)) {
                $CrisisLevelModel->where('id', 'in', $id)->update([
                    'status' => $status
                ]);
            } else {
                $CrisisLevelModel->where('id', '=', $id)->update([
                    'status' => $status
                ]);
            }
            return success([], '修改成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 异常行为表现列表
     * @return bool|string
     */
    public function CrisisBehaviorList(): bool|string
    {
        try {
            $param = $this->request->param();
            $CrisisBehaviorModel = new CrisisBehaviorModel();
            $query = $CrisisBehaviorModel->where('unit_id', '=', $this->userData['unit_id']);
            $data = $query->with([
                'user' => function ($query) {
                    $query->with(['roleName']);
                }
            ])->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$item) {
                if (empty($item['role_name'])) {
                    $item['role_name'] = '超级管理员';
                }
            }
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 异常行为表现添加
     * @return bool|string
     */
    public function CrisisBehaviorAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'name|名称' => 'require',
                'explain|说明' => 'require'
            ]);
            $CrisisBehaviorModel = new CrisisBehaviorModel();
            if ($CrisisBehaviorModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['name', '=', $param['name']]
            ])->value('id')) {
                throw new \Exception('不可重名！');
            }
            $param['unit_id'] = $this->userData['unit_id'];
            $param['uid'] = $this->userData['id'];
            $param['time'] = time();
            $CrisisBehaviorModel->insert($param);
            return success([], '添加成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 异常行为表现详情
     * @return bool|string
     */
    public function CrisisBehaviorInfo(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisBehaviorModel = new CrisisBehaviorModel();
            $data = $CrisisBehaviorModel->find($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 异常行为表现编辑
     * @return bool|string
     */
    public function CrisisBehaviorUpdate(): bool|string
    {
        try {
            $param = $this->request->param();
            $CrisisBehaviorModel = new CrisisBehaviorModel();
            $info = $CrisisBehaviorModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if (!empty($param['name']) && $param['name'] != $info['name']) {
                if ($CrisisBehaviorModel->where([
                    ['unit_id', '=', $this->userData['unit_id']],
                    ['name', '=', $param['name']]
                ])->value('id')) {
                    throw new \Exception('不可重名！');
                }
            }
            $info->save($param);
            return success([], '修改成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 异常行为表现删除
     * @return bool|string
     */
    public function CrisisBehaviorDel(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisBehaviorModel = new CrisisBehaviorModel();
            if (is_array($id)) {
                $CrisisBehaviorModel->where('id', 'in', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            } else {
                $CrisisBehaviorModel->where('id', '=', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }
            return success([], '删除成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 异常行为表现修改启用状态
     * @return bool|string
     */
    public function CrisisBehaviorStatus(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $status = $this->request->param('status');
            $CrisisBehaviorModel = new CrisisBehaviorModel();
            if (is_array($id)) {
                $CrisisBehaviorModel->where('id', 'in', $id)->update([
                    'status' => $status
                ]);
            } else {
                $CrisisBehaviorModel->where('id', '=', $id)->update([
                    'status' => $status
                ]);
            }
            return success([], '修改成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机上报
     * @return bool|string
     */
    public function CrisisReporting(): bool|string
    {
        $CrisisUserModel = new CrisisUserModel();
        $CrisisUserModel->startTrans();
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'name|危机用户名称' => 'require',
                'sex|危机用户性别' => 'require',
                'department_name|危机用户部门名称' => 'require',
                'problem|危机用户问题' => 'require',
                'behavior|危机用户表现行为' => 'require',
//                'informant|举报人身份' => 'require',
                'phone|联系方式' => 'require',
            ]);
            $is_disable = $CrisisUserModel->where([
                ['uid', '=', $this->userData['id']],
                ['is_disable', '>', 0]
            ])->field('is_disable,up_time')->select()->toArray();
            if (!empty($is_disable)) {
                foreach ($is_disable as $v) {
                    $day = match ($v['is_disable']) {
                        1 => 3,
                        2 => 7,
                        3 => 15,
                        4 => 30,
                    };
                    $time = $v['up_time'] + ((24 * 60 * 60) * $day);
                    if ($time > time()) throw new \Exception('恶意上报被禁用中~');
                }
            }
            $param['unit_id'] = $this->userData['unit_id'];
            $param['uid'] = $this->userData['id'];
            $param['time'] = time();
            $CrisisUserModel->insert($param);
            //消息记录盒子
            $CrisisLogModel = new CrisisLogModel();
            $CrisisLogModel->insert([
                'unit_id' => $this->userData['unit_id'],
                'uid' => 0,
                'type' => 1,
                'source_type' => 2,
                'behavior' => '危机上报待审核',
                'time' => time()
            ]);
            //系统消息
            $MessageData = [];
            $UnitModel = new UnitModel();
            $interfere_admin_status = $UnitModel->where('id', '=', $this->userData['unit_id'])->value('interfere_admin_status');
            if ($interfere_admin_status == 1) {
                $InterfereAdminModel = new InterfereAdminModel();
                $uids = $InterfereAdminModel->where('unit_id', '=', $this->userData['unit_id'])->distinct(true)->column('uid');
                foreach ($uids as $uid) {
                    $MessageData[] = [
                        'uid' => $uid,
                        'type' => 14,
                        'title' => '危机上报',
                        'content' => $this->userData['name'] . '于' . date('Y-m-d') . '日期' . date('H:i:s') . '时间向您上报了一起心理危机，请及时审核处理。',
                        'time' => time()
                    ];
                }
            } else {
                $UserModel = new NotDelUserModel();
                $uids = $UserModel->where([
                    ['unit_id', '=', $this->userData['unit_id']],
                    ['role_id', '=', 0]
                ])->distinct(true)->column('id');
                foreach ($uids as $uid) {
                    $MessageData[] = [
                        'uid' => $uid,
                        'type' => 14,
                        'title' => '危机上报',
                        'content' => $this->userData['name'] . '于' . date('Y-m-d') . '日期' . date('H:i:s') . '时间向您上报了一起心理危机，请及时审核处理。',
                        'time' => time()
                    ];
                }
            }
            if (count($MessageData) > 0) {
                $MessageModel = new MessageModel();
                $MessageModel->insertAll($MessageData);
            }
            $CrisisUserModel->commit();
            return success([], '提交成功！');
        } catch (\Exception $e) {
            $CrisisUserModel->rollback();
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机上报预检
     * @return bool|string
     */
    public function CrisisUserList(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['name'])) {
                $where[] = ['name', 'like', '%' . $param['name'] . '%'];
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['up_time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['up_time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['up_time', '<=', $param['end_time']];
                }
            }
            if (isset($param['status']) && strlen($param['status']) > 0) {
                $where[] = ['status', '=', $param['status']];
            }
            if (isset($param['is_complete']) && strlen($param['is_complete']) > 0) {
                if ($param['is_complete'] == 1) {
                    $where[] = ['status', '>', 0];
                } else {
                    $where[] = ['status', '=', 0];
                }
            }
            if (isset($param['uid']) && strlen($param['uid']) > 0) {
                $where[] = ['uid', '=', $param['uid']];
            }
            if ($this->userData['role_id'] > 0) {
                $DepartmentAdminModel = new DepartmentAdminModel();
                $department_ids = $DepartmentAdminModel->where('uid', '=', $this->userData['id'])->column('department_id');
                if (!empty($department_ids)) {
                    $UserModel = new NotDelUserModel();
                    $uid = $UserModel->where('department_id', 'in', $department_ids)->column('id');
                    if (!empty($uid)) {
                        $where[] = ['uid', 'in', $uid];
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                } else {
                    $where[] = ['id', '=', '-1'];
                }
            }
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $CrisisUserModel = new CrisisUserModel();
            $query = $CrisisUserModel->where($where);
            $data = $query->with([
                'user' => function ($query) {
                    $query->with(['unitName', 'roleName']);
                },
                'operator'
            ])->order('id desc')->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$item) {
                if (empty($item['role_name'])) $item['role_name'] = '超级管理员';
            }
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 上报人选择器
     * @return bool|string
     */
    public function ReportPersonSelector(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisUserModel = new CrisisUserModel();
            $data = $CrisisUserModel->where('unit_id', '=', $this->userData['unit_id'])->with([
                'user' => function ($query) {
                    $query->with(['roleName']);
                }
            ])->cache(true, 60)->select()->toArray();
            $data2 = [];
            if (isset($id) && strlen($id) > 0) {
                $uid = [];
                foreach ($data as $item) {
                    if ($item['role_id'] == $id) {
                        if (in_array($item['uid'], $uid)) continue;
                        $data2[] = [
                            'id' => $item['uid'],
                            'role_id' => $item['role_id'],
                            'name' => $item['user_name']
                        ];
                    }
                }
            } else {
                $role_mode = [];
                foreach ($data as $item) {
                    if (in_array($item['role_mode'], $role_mode)) continue;
                    if ($item['role_id'] == 0) $item['role_name'] = '超级管理员';
                    $role_mode[] = $item['role_mode'];
                    $data2[] = [
                        'id' => $item['role_id'],
                        'name' => $item['role_name'],
                    ];
                }
            }
            return success($data2);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机上报作废
     * @return bool|string
     */
    public function CrisisUserCancel(): bool|string
    {
        try {
//            if (!$this->isAuth()) throw new \Exception('您没有权限操作！');
            $id = $this->request->param('id');
            $CrisisUserModel = new CrisisUserModel();
            if (is_array($id)) {
                $CrisisUserModel->where('id', 'in', $id)->update([
                    'operator_uid' => $this->userData['id'],
                    'status' => 3,
                    'up_time' => time()
                ]);
            } else {
                $CrisisUserModel->where('id', '=', $id)->update([
                    'operator_uid' => $this->userData['id'],
                    'status' => 3,
                    'up_time' => time()
                ]);
            }
            return success([], '修改成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 删除危机上报记录
     * @return bool|string
     */
    public function CrisisUserDel(): bool|string
    {
        try {
//            if (!$this->isAuth()) throw new \Exception('您没有权限操作！');
            $id = $this->request->param('id');
            $CrisisUserModel = new CrisisUserModel();
            if (is_array($id)) {
                $CrisisUserModel->where('id', 'in', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            } else {
                $CrisisUserModel->where('id', '=', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }
            return success([], '删除成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机上报详情
     * @return bool|string
     */
    public function CrisisUserInfo(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisUserModel = new CrisisUserModel();
            $data = $CrisisUserModel->with([
                'user' => function ($query) {
                    $query->with(['departmentName', 'unitName', 'roleName']);
                },
                'operator'
            ])->find($id);
            if (!$data) throw new \Exception('参数错误！');
            $data = $data->toArray();
            if ($data['role_id'] == 0) {
                $data['role_name'] = '超级管理员';
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 处理预检-不存在危机
     * @return bool|string
     */
    public function CrisisUserUpdate2(): bool|string
    {
        try {
            if (!$this->isAuth()) throw new \Exception('您没有权限操作！');
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require|integer|max:11',
                'is_crisis|是否存在危机' => 'require',
                'is_malice|是否恶意上报' => 'require',
                'is_disable|禁用时间' => 'require',
            ]);
            $param['operator_uid'] = $this->userData['id'];
            $param['status'] = 3;
            $param['up_time'] = time();
            $CrisisUserModel = new CrisisUserModel();
            $info = $CrisisUserModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 处理预检
     * @return bool|string
     */
    public function CrisisUserUpdate(): bool|string
    {
        $CrisisUserModel = new CrisisUserModel();
        $CrisisUserModel->startTrans();
        try {
            if (!$this->isAuth()) throw new \Exception('您没有权限操作！');
            $param = $this->request->param('crisis');
            $this->validate($param, [
                'id|ID' => 'require|integer|max:11',
                'status|状态' => 'require'
            ]);
            $info = $CrisisUserModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $param['operator_uid'] = $this->userData['id'];
            $param['up_time'] = time();
            $info->save($param);
            switch ($param['status']) {
                case 1:
                    //移入关注人群
                    $follow_param = $this->request->param('follow');
                    $this->validate($follow_param, [
                        'uid|关注用户' => 'require|integer|max:11',
                        'level|关注等级' => 'require',
                        'reason|关注原因' => 'require',
                    ]);
                    $follow_param['unit_id'] = $this->userData['unit_id'];
                    $follow_param['operator_uid'] = $this->userData['id'];
                    $follow_param['channel'] = 1;
                    $follow_param['channel_id'] = $param['id'];
                    $follow_param['time'] = time();
                    $FollowUserModel = new FollowUserModel();
                    $FollowUserModel->insert($follow_param);
                    $log = [
                        'unit_id' => $this->userData['unit_id'],
                        'uid' => $follow_param['uid'],
                        'type' => 2,
                        'source_type' => 2,
                        'behavior' => '移入关注人群',
                        'time' => time()
                    ];
                    break;
                case 2:
                    //移入干预人群
                    $interfere_param = $this->request->param('interfere');
                    $this->validate($interfere_param, [
                        'uid|干预用户' => 'require|integer|max:11',
                        'describe|移入说明' => 'require',
                        'type_id|问题类型' => 'require|integer|max:11',
                        'level_id|风险等级评估' => 'require|integer|max:11',
                    ]);
                    $behavior_id = $interfere_param['behavior_id'] ?? [];
                    unset($interfere_param['behavior_id']);
                    $interfere_param['unit_id'] = $this->userData['unit_id'];
                    $interfere_param['operator_uid'] = $this->userData['id'];
                    $interfere_param['channel'] = 1;
                    $interfere_param['channel_id'] = $param['id'];
                    $interfere_param['move_status'] = 1;
                    $interfere_param['time'] = time();
                    $InterfereUserModel = new InterfereUserModel();
                    $interfere_id = $InterfereUserModel->insertGetId($interfere_param);
                    $InterfereBehaviorModel = new InterfereBehaviorModel();
                    $InterfereBehaviorModel->where('interfere_id', '=', $interfere_id)->update([
                        'is_del' => 1,
                        'del_time' => time()
                    ]);
                    if (!empty($behavior_id)) {
                        $BehaviorData = [];
                        foreach ($behavior_id as $v) {
                            $BehaviorData[] = [
                                'interfere_id' => $interfere_id,
                                'behavior_id' => $v,
                                'time' => time()
                            ];
                        }
                        $InterfereBehaviorModel->insertAll($BehaviorData);
                    }
                    $log = [
                        'unit_id' => $this->userData['unit_id'],
                        'uid' => $interfere_param['uid'],
                        'type' => 3,
                        'source_type' => 2,
                        'behavior' => '移入干预人群',
                        'time' => time()
                    ];
                    break;
                default:
                    throw new \Exception('参数错误！');
            }
            //消息记录盒子
            $CrisisLogModel = new CrisisLogModel();
            $CrisisLogModel->insert($log);
            $CrisisUserModel->commit();
            return success([], '更新成功！');
        } catch (\Exception $e) {
            $CrisisUserModel->rollback();
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 关注人群列表
     * @return bool|string
     */
    public function FollowUserList(): bool|string
    {
        try {
            $param = $this->request->param();
            $UserModel = new NotDelUserModel();
            $where = [];
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['level'])) {
                $where[] = ['level', '=', $param['level']];
            }
            if (!empty($param['up_num'])) {
                $where[] = ['up_num', '=', $param['up_num']];
            }
            if (!empty($param['channel'])) {
                $where[] = ['channel', '=', $param['channel']];
            }
            if (!empty($param['operator_uid'])) {
                $where[] = ['operator_uid', '=', $param['operator_uid']];
            }
            if (!empty($param['department_id']) && !empty($param['name'])) {
                $uids = $UserModel->where([
                    ['name|username', 'like', '%' . $param['name'] . '%'],
                    ['department_id', '=', $param['department_id']],
                ])->column('id');
                if (count($uids) > 0) {
                    $where[] = ['uid', 'in', $uids];
                } else {
                    $where[] = ['id', '=', '-1'];
                }
            } else {
                if (!empty($param['name'])) {
                    $uids = $UserModel->where('name|username', 'like', '%' . $param['name'] . '%')->column('id');
                    if (count($uids) > 0) {
                        $where[] = ['uid', 'in', $uids];
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                }
                if (!empty($param['department_id'])) {
                    $uids = $UserModel->where('department_id', '=', $param['department_id'])->column('id');
                    if (count($uids) > 0) {
                        $where[] = ['uid', 'in', $uids];
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                }
            }
            if (!empty($param['uid'])) {
                $where[] = ['uid', '=', $param['uid']];
            }
            if (!empty($param['id'])) {
                $where[] = ['id', 'in', $param['id']];
            }
            switch ($this->userData['role_mode']) {
                case 1:
                    $DepartmentAdminModel = new DepartmentAdminModel();
                    $department_ids = $DepartmentAdminModel->where('uid', '=', $this->userData['id'])->column('department_id');
                    if (count($department_ids) > 0) {
                        $uid = $UserModel->where('department_id', 'in', $department_ids)->column('id');
                        if (!empty($uid)) {
                            $where[] = ['uid', 'in', $uid];
                        } else {
                            $where[] = ['id', '=', '-1'];
                        }
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                    break;
                case 2:
                    $ConsultOfflineModel = new ConsultOfflineModel();
                    $ConsultOnlineModel = new ConsultOnlineModel();
                    $uid = $ConsultOnlineModel->where('consult_id', '=', $this->userData['id'])->column('uid');
                    $uid2 = $ConsultOfflineModel->where('consult_id', '=', $this->userData['id'])->column('uid');
                    $uid3 = array_merge($uid, $uid2);
                    if (!empty($uid3)) {
                        $uid3 = array_unique($uid3);
                        $where[] = ['uid', 'in', $uid3];
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                    break;
                case 3:
                    $where[] = ['id', '=', -1];
            }
            if (isset($param['is_end']) && strlen($param['is_end'])) {
                $where[] = ['is_end', '=', $param['is_end']];
            }
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $where[] = ['status', '=', 0];
            $FollowUserModel = new FollowUserModel();
            $query = $FollowUserModel->where($where);
            $data = $query->with([
                'operator',
                'user' => function ($query) {
                    $query->with([
                        'department' => function ($query) {
                            $query->with([
                                'departmentAdmins' => function ($query) {
                                    $query->with(['userName']);
                                }
                            ]);
                        },
                        'unitName']);
                }
            ])->order('id desc')->paginate($param['pageSize'])->toArray();
            $TaskUserResultModel = new TaskUserResultModel();
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $WirelessEvaluationResultModel = new WirelessEvaluationResultModel();
            $ConsultOnlineResultModel = new ConsultOnlineResultModel();
            $ConsultOfflineResultModel = new ConsultOfflineResultModel();
            $GroupActivityTaskUserResultModel = new GroupActivityTaskUserResultModel();
            $TrainingTaskUserResultModel = new TrainingTaskUserResultModel();
            $UserService = new UserService($this->app);
            $NotDelDepartmentAll = $UserService->getNotDelDepartmentAffiliationAll($this->userData['unit_id']);
            foreach ($data['data'] as &$item) {
                $item['department_name'] = $NotDelDepartmentAll[$item['department_id']]['all_name'] ?? '';
                $model = match ($item['channel']) {
                    2 => $TaskUserResultModel,
                    3 => $SelfEvaluationResultModel,
                    4 => $ProxyEvaluationResultModel,
                    5 => $WirelessEvaluationResultModel,
                    6 => $ConsultOnlineResultModel,
                    7 => $ConsultOfflineResultModel,
                    11 => $GroupActivityTaskUserResultModel,
                    12 => $TrainingTaskUserResultModel,
                    default => false
                };
                if (!$model) continue;
                $result = $model->with([
                    'RatingScaleFactorWarningModel',
                    'RatingScale' => function ($query) {
                        $query->with(['ratingScaleTypeName']);
                    }
                ])->find($item['channel_id']);
                if (!$result) continue;
                $result = $result->toArray();
                $item['rating_scale_name'] = $result['rating_scale_name'];
                $item['warning_name'] = $result['warning_name'];
                $item['result_read_auth'] = $result['result_read_auth'] ?? '';
                $item['total_score'] = $result['total_score'];
                $item['is_explain'] = $result['is_explain'] ?? 0;
                $item['rating_scale_type_name'] = $result['rating_scale_type_name'];
                $item['answer_role'] = $result['answer_role'] ?? '';
                switch ($item['channel']) {
                    case 6:
                        $item['answer_role'] = 1;
                        $ConsultOnlineMessageModel = new ConsultOnlineMessageModel();
                        $result_read_auth = $ConsultOnlineMessageModel->where('id', '=', $result['source_id'])->value('result_read_auth');
                        if ($result_read_auth == 1) {
                            $item['result_read_auth'] = '1,2,3,4';
                        } else {
                            $item['result_read_auth'] = '1,2';
                        }
                        break;
                    case 7:
                        $item['answer_role'] = 1;
                        $ConsultOfflineTaskModel = new ConsultOfflineTaskModel();
                        $result_read_auth = $ConsultOfflineTaskModel->where('id', '=', $result['source_id'])->value('result_read_auth');
                        if ($result_read_auth == 1) {
                            $item['result_read_auth'] = '1,2,3,4';
                        } else {
                            $item['result_read_auth'] = '1,2';
                        }
                        break;
                }
            }

            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 删除关注人群
     * @return bool|string
     */
    public function FollowUserDel(): bool|string
    {
        try {
//            if (!$this->isAuth()) throw new \Exception('您没有权限操作！');
            $id = $this->request->param('id');
            $FollowUserModel = new FollowUserModel();
            if (is_array($id)) {
                $FollowUserModel->where('id', 'in', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            } else {
                $FollowUserModel->where('id', '=', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }
            return success([], '删除成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 关注人群详情
     * @return bool|string
     */
    public function FollowUserInfo(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $FollowUserModel = new FollowUserModel();
            $data = $FollowUserModel->with([
                'operator' => function ($query) {
                    $query->with(['roleName']);
                },
                'user' => function ($query) {
                    $query->with(['departmentName', 'unitName']);
                }
            ])->find($id);
            if (!$data) throw new \Exception('参数错误！');
            /**
             * ['id' => 1, 'name' => '危机上报手动移入'],
             * ['id' => 2, 'name' => '任务测评自动加入'],
             * ['id' => 3, 'name' => '自我测评自动加入'],
             * ['id' => 4, 'name' => '他人代测自动移入'],
             * ['id' => 5, 'name' => '无线测评自动移入'],
             * ['id' => 6, 'name' => '线上咨询手动移入'],
             * ['id' => 7, 'name' => '线下预约手动移入'],
             * ['id' => 8, 'name' => '关注人群手动导入'],
             * ['id' => 9, 'name' => '干预人群手动导入'],
             * */
            switch ($data['channel']) {
                case 2:
                    $ReportService = new ReportService($this->app);
                    $data['result'] = $ReportService->getTaskReportInfo($data['channel_id']);
                    break;
                case 3:
                    $ReportService = new ReportService($this->app);
                    $data['result'] = $ReportService->getSelfReportInfo($data['channel_id']);
                    break;
                case 4:
                    $ReportService = new ReportService($this->app);
                    $data['result'] = $ReportService->getProxyReportInfo($data['channel_id']);
                    break;
//                case 6:
//                    $ReportService = new ReportService($this->app);
//                    $data['result'] = $ReportService->OnlineReportInfo($data['channel_id']);
//                    break;
//                case 7:
//                    $ReportService = new ReportService($this->app);
//                    $data['result'] = $ReportService->OfflineReportInfo($data['channel_id']);
//                    break;

                case 6:
                    $ConsultOnlineResultModel = new ConsultOnlineResultModel();
                    $result = $ConsultOnlineResultModel->with([
                        'RatingScaleFactorWarningModel',
                        'RatingScale' => function ($query) {
                            $query->with(['ratingScaleTypeName']);
                        }
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $ConsultOnlineMessageModel = new ConsultOnlineMessageModel();
                    $data['log_id'] = $ConsultOnlineMessageModel->where('id', '=', $result['source_id'])->value('online_id');
                    break;
                case 7:
                    $ConsultOfflineResultModel = new ConsultOfflineResultModel();
                    $result = $ConsultOfflineResultModel->with([
                        'RatingScaleFactorWarningModel',
                        'RatingScale' => function ($query) {
                            $query->with(['ratingScaleTypeName']);
                        }
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $ConsultOfflineTaskModel = new ConsultOfflineTaskModel();
                    $data['log_id'] = $ConsultOfflineTaskModel->where('id', '=', $result['source_id'])->value('offline_id');
                    break;
                case 13:
                    $data['log_id'] = $data['channel_id'];
            }
            $data['up_content'] = $data['up_content'] ? json_decode($data['up_content'], true) : [];
            $data['operator_role_name'] = $data['operator_role_name'] ?? '超级管理员';
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 关注人群编辑
     * @return bool|string
     */
    public function FollowUserSave(): bool|string
    {
        try {
//            if (!$this->isAuth()) throw new \Exception('您没有权限操作！');
            $param = $this->request->param();
            $FollowUserModel = new FollowUserModel();
            $info = $FollowUserModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if (empty($param['is_end'])) {
                if (!empty($param['up_content'])) {
                    $param['up_num'] = count($param['up_content']);
                    $param['up_content'] = json_encode($param['up_content'], true);
                } else {
                    $param['up_num'] = 0;
                    $param['up_content'] = '';
                }
            }
//            $param['operator_uid'] = $this->userData['id'];
            $info->save($param);
            return success([], '编辑成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预人群列表
     * @return bool|string
     */
    public function InterfereUserList(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            $UserModel = new NotDelUserModel();
            if (!empty($param['department_id']) && !empty($param['name'])) {
                $uids = $UserModel->where([
                    ['name|username', 'like', '%' . $param['name'] . '%'],
                    ['department_id', '=', $param['department_id']],
                ])->column('id');
                if (count($uids) > 0) {
                    $where[] = ['uid', 'in', $uids];
                } else {
                    $where[] = ['id', '=', '-1'];
                }
            } else {
                if (!empty($param['name'])) {
                    $uids = $UserModel->where('name|username', 'like', '%' . $param['name'] . '%')->column('id');
                    if (count($uids) > 0) {
                        $where[] = ['uid', 'in', $uids];
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                }
                if (!empty($param['department_id'])) {
                    $uids = $UserModel->where('department_id', '=', $param['department_id'])->column('id');
                    if (count($uids) > 0) {
                        $where[] = ['uid', 'in', $uids];
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                }
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['move_time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['move_time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['move_time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['type_id'])) {
                $where[] = ['type_id', '=', $param['type_id']];
            }
            if (!empty($param['level_id'])) {
                $where[] = ['level_id', '=', $param['level_id']];
            }
            if (!empty($param['behavior_id'])) {
                $InterfereBehaviorModel = new InterfereBehaviorModel();
                $ids = $InterfereBehaviorModel->where('behavior_id', '=', $param['behavior_id'])->column('interfere_id');
                if (count($ids) > 0) {
                    $where[] = ['id', 'in', $ids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['channel'])) {
                $where[] = ['channel', '=', $param['channel']];
            }
            if (!empty($param['uid'])) {
                $where[] = ['uid', '=', $param['uid']];
            }
            if (!empty($param['id'])) {
                $where[] = ['id', 'in', $param['id']];
            }
            switch ($this->userData['role_mode']) {
                case 1:
                    $DepartmentAdminModel = new DepartmentAdminModel();
                    $department_ids = $DepartmentAdminModel->where('uid', '=', $this->userData['id'])->column('department_id');
                    if (count($department_ids) > 0) {
                        $uid = $UserModel->where('department_id', 'in', $department_ids)->column('id');
                        if (!empty($uid)) {
                            $where[] = ['uid', 'in', $uid];
                        } else {
                            $where[] = ['id', '=', '-1'];
                        }
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                    break;
                case 2:
                    $ConsultOfflineModel = new ConsultOfflineModel();
                    $ConsultOnlineModel = new ConsultOnlineModel();
                    $uid = $ConsultOnlineModel->where('consult_id', '=', $this->userData['id'])->column('uid');
                    $uid2 = $ConsultOfflineModel->where('consult_id', '=', $this->userData['id'])->column('uid');
                    $uid3 = array_merge($uid, $uid2);
                    if (!empty($uid3)) {
                        $uid3 = array_unique($uid3);
                        $where[] = ['uid', 'in', $uid3];
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                    break;
                case 3:
                    $where[] = ['id', '=', -1];
            }
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $where[] = ['move_status', '=', 1];
            $where[] = ['status', '=', 0];
            $InterfereUserModel = new InterfereUserModel();
            $query = $InterfereUserModel->where($where);
            $data = $query->with([
                'user' => function ($query) {
                    $query->with([
                        'department' => function ($query) {
                            $query->with([
                                'departmentAdmins' => function ($query) {
                                    $query->with(['userName']);
                                }
                            ]);
                        },
                        'unitName'
                    ]);
                },
                'InterfereBehavior' => function ($query) {
                    $query->with(['CrisisBehavior']);
                },
                'OperatorUser' => function ($query) {
                    $query->with(['roleName']);
                }
            ])->order('id desc')->paginate($param['pageSize'])->toArray();
            if (count($data['data']) > 0) {
                $departmentIds = array_column($data['data'], 'department_id');
                $UserService = new UserService($this->app);
                $NotDelDepartmentAll = $UserService->getDepartmentAffiliationAll2($this->userData['unit_id'], $departmentIds);
                foreach ($data['data'] as &$item) {
                    //部门归属
                    $item['department_name'] = $NotDelDepartmentAll['department_data'][$item['department_id']]['all_name'] ?? '';
                    $item['ascription_name'] = $NotDelDepartmentAll['department_data'][$item['department_id']]['ascription_name'] ?? '';
                    $item['operator_role_name'] = $item['operator_role_name'] ?? "超级管理员";
                }
            }
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 移入干预预检列表
     * @return bool|string
     */
    public function MoveInterfereUserList(): bool|string
    {
        try {
            $UserModel = new NotDelUserModel();
            $param = $this->request->param();
            $where = [];
            if (!empty($param['name'])) {
                $uids = $UserModel->where('name', 'like', '%' . $param['name'] . '%')->column('id');
                if (count($uids) > 0) {
                    $where[] = ['uid', 'in', $uids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['move_time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['move_time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['move_time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['type_id'])) {
                $where[] = ['type_id', '=', $param['type_id']];
            }
            if (!empty($param['level_id'])) {
                $where[] = ['level_id', '=', $param['level_id']];
            }
            if (!empty($param['status'])) {
                $where[] = ['move_status', '=', $param['status']];
            }
            if (!empty($param['role_id'])) {
                //根据角色ID，查询角色对应的用户ID
                $operator_uid = $UserModel->where('role_id', $param['role_id'])->column('id');
                $where[] = ['operator_uid', 'in', $operator_uid];
            }
            if (!empty($param['uid'])) {
                $where[] = ['operator_uid', '=', $param['uid']];
            }
            switch ($this->userData['role_mode']) {
                case 1:
//                    $DepartmentAdminModel = new DepartmentAdminModel();
//                    $department_ids = $DepartmentAdminModel->where('uid', '=', $this->userData['id'])->column('department_id');
//                    if (count($department_ids) > 0) {
//                        $uid = $UserModel->where('department_id', 'in', $department_ids)->column('id');
//                        if (!empty($uid)) {
//                            $where[] = ['uid', 'in', $uid];
//                        } else {
//                            $where[] = ['id', '=', '-1'];
//                        }
//                    } else {
//                        $where[] = ['id', '=', '-1'];
//                    }
                    break;
                case 2:
                    $ConsultOfflineModel = new ConsultOfflineModel();
                    $ConsultOnlineModel = new ConsultOnlineModel();
                    $uid = $ConsultOnlineModel->where('consult_id', '=', $this->userData['id'])->column('uid');
                    $uid2 = $ConsultOfflineModel->where('consult_id', '=', $this->userData['id'])->column('uid');
                    $uid3 = array_merge($uid, $uid2);
                    if (!empty($uid3)) {
                        $uid3 = array_unique($uid3);
                        $where[] = ['uid', 'in', $uid3];
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                    break;
                case 3:
                    $where[] = ['id', '=', -1];
            }
            $where[] = ['channel', 'in', [1, 2, 3, 4, 5, 6, 7, 8]];
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $InterfereUserModel = new InterfereUserModel();
            $query = $InterfereUserModel->where($where);
            $data = $query->with([
                'user' => function ($query) {
                    $query->with([
                        'unitName',
                        'roleName',
                        'department' => function ($query) {
                            $query->with([
                                'departmentAdmins' => function ($query) {
                                    $query->with(['userName']);
                                }
                            ]);
                        }
                    ]);
                },
                'OperatorUser' => function ($query) {
                    $query->with(['roleName']);
                },
                'applicantUser',
                'FollowUser' => function ($query) {
                    $query->with(['operator']);
                }
            ])->order('id desc')->paginate($param['pageSize'])->toArray();
            $departmentIds = array_column($data['data'], 'department_id');
            $UserService = new UserService($this->app);
            $NotDelDepartmentAll = $UserService->getDepartmentAffiliationAll2($this->userData['unit_id'], $departmentIds);
            $TaskUserResultModel = new TaskUserResultModel();
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $ConsultOnlineResultModel = new ConsultOnlineResultModel();
            $ConsultOfflineResultModel = new ConsultOfflineResultModel();
            $GroupActivityTaskUserResultModel = new GroupActivityTaskUserResultModel();
            $TrainingTaskUserResultModel = new TrainingTaskUserResultModel();
            foreach ($data['data'] as &$item) {
                $item['department_name'] = $NotDelDepartmentAll['department_data'][$item['department_id']]['all_name'] ?? '';
                switch ($item['channel']) {
                    case 2:
                        $result = $TaskUserResultModel->with([
                            'RatingScaleFactorWarningModel',
                            'RatingScale' => function ($query) {
                                $query->with(['ratingScaleTypeName']);
                            }
                        ])->find($item['channel_id']);
                        if (!$result) break;
                        $item['rating_scale_name'] = $result['rating_scale_name'];
                        $item['warning_name'] = $result['warning_name'];
                        $item['result_read_auth'] = $result['result_read_auth'];
                        $item['total_score'] = $result['total_score'];
                        $item['is_explain'] = $result['is_explain'];
                        $item['rating_scale_type_name'] = $result['rating_scale_type_name'];
                        $item['answer_role'] = $result['answer_role'];
                        break;
                    case 3:
                        $result = $SelfEvaluationResultModel->with([
                            'RatingScaleFactorWarningModel',
                            'RatingScale' => function ($query) {
                                $query->with(['ratingScaleTypeName']);
                            }
                        ])->find($item['channel_id']);
                        if (!$result) break;
                        $item['rating_scale_name'] = $result['rating_scale_name'];
                        $item['warning_name'] = $result['warning_name'];
                        $item['result_read_auth'] = $result['result_read_auth'];
                        $item['total_score'] = $result['total_score'];
                        $item['is_explain'] = $result['is_explain'];
                        $item['rating_scale_type_name'] = $result['rating_scale_type_name'];
                        $item['answer_role'] = $result['answer_role'];
                        break;
                    case 4:
                        $result = $ProxyEvaluationResultModel->with([
                            'RatingScaleFactorWarningModel',
                            'RatingScale' => function ($query) {
                                $query->with(['ratingScaleTypeName']);
                            }
                        ])->find($item['channel_id']);
                        if (!$result) break;
                        $item['rating_scale_name'] = $result['rating_scale_name'];
                        $item['warning_name'] = $result['warning_name'];
                        $item['result_read_auth'] = $result['result_read_auth'];
                        $item['total_score'] = $result['total_score'];
                        $item['is_explain'] = $result['is_explain'];
                        $item['rating_scale_type_name'] = $result['rating_scale_type_name'];
                        $item['answer_role'] = $result['answer_role'];
                        break;
                    case 6:
                        $result = $ConsultOnlineResultModel->with([
                            'RatingScaleFactorWarningModel',
                            'RatingScale' => function ($query) {
                                $query->with(['ratingScaleTypeName']);
                            }
                        ])->find($item['channel_id']);
                        if (!$result) break;
                        $ConsultOnlineMessageModel = new ConsultOnlineMessageModel();
                        $result_read_auth = $ConsultOnlineMessageModel->where('id', '=', $result['source_id'])->value('result_read_auth');
                        if ($result_read_auth == 1) {
                            $item['result_read_auth'] = '1,2,3,4';
                        } else {
                            $item['result_read_auth'] = '1,2';
                        }
                        $item['rating_scale_name'] = $result['rating_scale_name'];
                        $item['warning_name'] = $result['warning_name'];
                        $item['total_score'] = $result['total_score'];
                        $item['is_explain'] = $result['is_explain'];
                        $item['rating_scale_type_name'] = $result['rating_scale_type_name'];
                        $item['answer_role'] = 1;
                        break;
                    case 7:
                        $result = $ConsultOfflineResultModel->with([
                            'RatingScaleFactorWarningModel',
                            'RatingScale' => function ($query) {
                                $query->with(['ratingScaleTypeName']);
                            }
                        ])->find($item['channel_id']);
                        if (!$result) break;
                        $ConsultOfflineTaskModel = new ConsultOfflineTaskModel();
                        $result_read_auth = $ConsultOfflineTaskModel->where('id', '=', $result['source_id'])->value('result_read_auth');
                        if ($result_read_auth == 1) {
                            $item['result_read_auth'] = '1,2,3,4';
                        } else {
                            $item['result_read_auth'] = '1,2';
                        }
                        $item['rating_scale_name'] = $result['rating_scale_name'];
                        $item['warning_name'] = $result['warning_name'];
                        $item['total_score'] = $result['total_score'];
                        $item['is_explain'] = $result['is_explain'];
                        $item['rating_scale_type_name'] = $result['rating_scale_type_name'];
                        $item['answer_role'] = 1;
                        break;
                    case 11:
                        $result = $GroupActivityTaskUserResultModel->with([
                            'RatingScaleFactorWarningModel',
                            'RatingScale' => function ($query) {
                                $query->with(['ratingScaleTypeName']);
                            }
                        ])->find($item['channel_id']);
                        if (!$result) break;
                        $item['rating_scale_name'] = $result['rating_scale_name'];
                        $item['warning_name'] = $result['warning_name'];
                        $item['result_read_auth'] = $result['result_read_auth'];
                        $item['total_score'] = $result['total_score'];
                        $item['is_explain'] = $result['is_explain'] ?? 0;
                        $item['rating_scale_type_name'] = $result['rating_scale_type_name'];
                        $item['answer_role'] = $result['answer_role'];
                        break;
                    case 12:
                        $result = $TrainingTaskUserResultModel->with([
                            'RatingScaleFactorWarningModel',
                            'RatingScale' => function ($query) {
                                $query->with(['ratingScaleTypeName']);
                            }
                        ])->find($item['channel_id']);
                        if (!$result) break;
                        $item['rating_scale_name'] = $result['rating_scale_name'];
                        $item['warning_name'] = $result['warning_name'];
                        $item['result_read_auth'] = $result['result_read_auth'];
                        $item['total_score'] = $result['total_score'];
                        $item['is_explain'] = $result['is_explain'] ?? 0;
                        $item['rating_scale_type_name'] = $result['rating_scale_type_name'];
                        $item['answer_role'] = $result['answer_role'];
                        break;
                    default:
                        $item['rating_scale_name'] = '';
                        $item['warning_name'] = '';
                        $item['result_read_auth'] = '';
                        $item['total_score'] = '';
                        $item['is_explain'] = '';
                        $item['rating_scale_type_name'] = '';
                        $item['answer_role'] = '';
                }
                $item['answer_role'] = match ($item['answer_role'] ?? 0) {
                    1 => '用户',
                    2 => '家长',
                    default => '--'
                };
            }
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 移入干预预检-详情
     * @return bool|string
     */
    public function MoveInterfereUserInfo(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereUserModel = new InterfereUserModel();
            $data = $InterfereUserModel->with([
                'OperatorUser' => function ($query) {
                    $query->with(['roleName']);
                },
                'user' => function ($query) {
                    $query->with([
                        'unitName',
                        'department' => function ($query) {
                            $query->with([
                                'departmentAdmins' => function ($query) {
                                    $query->with(['userName']);
                                }
                            ]);
                        }
                    ]);
                },
                'InterfereBehavior' => function ($query) {
                    $query->with(['CrisisBehavior']);
                },
                'FollowUser' => function ($query) {
                    $query->with(['operator']);
                }
            ])->find($id);
            if (!$data) throw new \Exception('参数错误！');
            switch ($data['channel']) {
                case 2:
                    $TaskUserResultModel = new TaskUserResultModel();
                    $result = $TaskUserResultModel->with([
                        'RatingScaleFactorWarningModel' => function ($query) {
                            $query->with(['factorName']);
                        },
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $data['factor_name'] = $result['factor_name'];
                    $data['warning_name'] = $result['warning_name'];
                    break;
                case 3:
                    $SelfEvaluationResultModel = new SelfEvaluationResultModel();
                    $result = $SelfEvaluationResultModel->with([
                        'RatingScaleFactorWarningModel' => function ($query) {
                            $query->with(['factorName']);
                        },
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $data['factor_name'] = $result['factor_name'];
                    $data['warning_name'] = $result['warning_name'];
                    break;
                case 4:
                    $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
                    $result = $ProxyEvaluationResultModel->with([
                        'RatingScaleFactorWarningModel' => function ($query) {
                            $query->with(['factorName']);
                        },
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $data['factor_name'] = $result['factor_name'];
                    $data['warning_name'] = $result['warning_name'];
                    break;
                case 6:
                    $ConsultOnlineResultModel = new ConsultOnlineResultModel();
                    $result = $ConsultOnlineResultModel->with([
                        'RatingScaleFactorWarningModel' => function ($query) {
                            $query->with(['factorName']);
                        },
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $data['factor_name'] = $result['factor_name'];
                    $data['warning_name'] = $result['warning_name'];
                    $ConsultOnlineMessageModel = new ConsultOnlineMessageModel();
                    $data['log_id'] = $ConsultOnlineMessageModel->where('id', '=', $result['source_id'])->value('online_id');
                    break;
                case 7:
                    $ConsultOfflineResultModel = new ConsultOfflineResultModel();
                    $result = $ConsultOfflineResultModel->with([
                        'RatingScaleFactorWarningModel' => function ($query) {
                            $query->with(['factorName']);
                        },
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $data['factor_name'] = $result['factor_name'];
                    $data['warning_name'] = $result['warning_name'];
                    $ConsultOfflineTaskModel = new ConsultOfflineTaskModel();
                    $data['log_id'] = $ConsultOfflineTaskModel->where('id', '=', $result['source_id'])->value('offline_id');
                    break;
                default:
                    $data['factor_name'] = '';
                    $data['warning_name'] = '';
            }
            $data['conversation_content'] = $data['conversation_content'] ? json_decode($data['conversation_content'], true) : [];
            $data['evaluate_content'] = $data['evaluate_content'] ? json_decode($data['evaluate_content'], true) : [];
            $data['event_content'] = $data['event_content'] ? json_decode($data['event_content'], true) : [];
            $data['up_content'] = $data['up_content'] ? json_decode($data['up_content'], true) : [];
            $data['follow_up_content'] = $data['follow_up_content'] ? json_decode($data['follow_up_content'], true) : [];
            $data['operator_role_name'] = $data['operator_role_name'] ?? '超级管理员';
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预人群删除
     * @return bool|string
     */
    public function InterfereUserDel(): bool|string
    {
        $id = $this->request->param('id');
        try {
            $InterfereUserModel = new InterfereUserModel();
            if (is_array($id)) {
                $InterfereUserModel->where('id', 'in', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            } else {
                $InterfereUserModel->where('id', '=', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }
            return success([], '删除成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预人群完成干预
     * @return bool|string
     */
    public function InterfereUserStatus(): bool|string
    {
        $id = $this->request->param('id');
        $status = $this->request->param('status');
        $InterfereUserModel = new InterfereUserModel();
        $InterfereUserModel->startTrans();
        try {
            $info = $InterfereUserModel->find($id);
            if (!$info) throw new \Exception('参数错误！');
            $num = $InterfereUserModel->where([
                ['uid', '=', $info->uid],
                ['status', '=', 1]
            ])->count();
            $UserModel = new NotDelUserModel();
            //更新用户表，档案更新时间
            $UserModel->where('id', '=', $info->uid)->update(['archive_update_time' => time()]);
            $info->status = $status;
            $info->status_num = $num + 1;
            $info->status_time = time();
            $info->save();
            $InterfereUserModel->commit();
            return success([], '修改成功！');
        } catch (\Exception $e) {
            $InterfereUserModel->rollback();
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 移入干预人群审核
     * @return bool|string
     */
    public function InterfereUserMoveStatus(): bool|string
    {
        $InterfereUserModel = new InterfereUserModel();
        $InterfereUserModel->startTrans();
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require|integer|max:11',
                'status|状态' => 'require',
            ]);
            $info = $InterfereUserModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if ($info['operator_uid'] != $this->userData['id'] && $this->userData['role_id'] != 0) throw new \Exception('您没有操作权限！');
            if ($param['status'] == 1) {
                $FollowUserModel = new FollowUserModel();
                $FollowInfo = $FollowUserModel->with(['userName'])->find($info->follow_id);
                if ($FollowInfo) {
                    $FollowInfo->status = 1;
                    $FollowInfo->save();
                    $MessageModel = new MessageModel();
                    $MessageModel->insert([
                        'uid' => $FollowInfo['operator_uid'],
                        'type' => 15,
                        'title' => '干预预检通过',
                        'content' => '您之前申请移入干预人群的' . $FollowInfo['user_name'] . '用户，已通过了预检审核被移入了干预人群。',
                        'time' => time()
                    ]);
                }
            }
            $info->move_status = $param['status'];
            $info->move_time = time();
            if (!empty($param['priority'])) {
                $info->priority = $param['priority'];
            }
            $info->save();
            $InterfereUserModel->commit();
            return success([], '修改成功！');
        } catch (\Exception $e) {
            $InterfereUserModel->rollback();
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预人群详情
     * @return bool|string
     */
    public function InterfereUserInfo(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereUserModel = new InterfereUserModel();
            $data = $InterfereUserModel->with([
                'OperatorUser' => function ($query) {
                    $query->with(['roleName']);
                },
                'user' => function ($query) {
                    $query->with([
                        'role_name',
                        'unitName',
                        'department' => function ($query) {
                            $query->with([
                                'departmentAdmins' => function ($query) {
                                    $query->with(['userName']);
                                }
                            ]);
                        }
                    ]);
                },
                'InterfereBehavior' => function ($query) {
                    $query->with(['CrisisBehavior']);
                },
                'FollowUser' => function ($query) {
                    $query->with([
                        'operator' => function ($query) {
                            $query->with(['roleName']);
                        }
                    ]);
                },
                'opinionUser',
                'opinionUser2',
                'opinionUser3',
                'evaluateUser',
                'moveUser'
            ])->find($id);
            if (!$data) throw new \Exception('参数错误！');
            $data = $data->toArray();
            switch ($data['channel']) {
                case 2:
                    $TaskUserResultModel = new TaskUserResultModel();
                    $result = $TaskUserResultModel->with([
                        'RatingScaleFactorWarningModel' => function ($query) {
                            $query->with(['factorName']);
                        },
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $data['factor_name'] = $result['factor_name'];
                    $data['warning_name'] = $result['warning_name'];
                    break;
                case 3:
                    $SelfEvaluationResultModel = new SelfEvaluationResultModel();
                    $result = $SelfEvaluationResultModel->with([
                        'RatingScaleFactorWarningModel' => function ($query) {
                            $query->with(['factorName']);
                        },
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $data['factor_name'] = $result['factor_name'];
                    $data['warning_name'] = $result['warning_name'];
                    break;
                case 4:
                    $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
                    $result = $ProxyEvaluationResultModel->with([
                        'RatingScaleFactorWarningModel' => function ($query) {
                            $query->with(['factorName']);
                        },
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $data['factor_name'] = $result['factor_name'];
                    $data['warning_name'] = $result['warning_name'];
                    break;
                case 6:
                    $ConsultOnlineResultModel = new ConsultOnlineResultModel();
                    $result = $ConsultOnlineResultModel->with([
                        'RatingScaleFactorWarningModel' => function ($query) {
                            $query->with(['factorName']);
                        },
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $data['factor_name'] = $result['factor_name'];
                    $data['warning_name'] = $result['warning_name'];
                    $ConsultOnlineMessage = new ConsultOnlineMessageModel();
                    $data['log_id'] = $ConsultOnlineMessage->where('id', '=', $result['source_id'])->value('online_id');
                    break;
                case 7:
                    $ConsultOfflineResultModel = new ConsultOfflineResultModel();
                    $result = $ConsultOfflineResultModel->with([
                        'RatingScaleFactorWarningModel' => function ($query) {
                            $query->with(['factorName']);
                        },
                    ])->find($data['channel_id']);
                    if (!$result) break;
                    $data['factor_name'] = $result['factor_name'];
                    $data['warning_name'] = $result['warning_name'];
                    $ConsultOfflineTaskModel = new ConsultOfflineTaskModel();
                    $data['log_id'] = $ConsultOfflineTaskModel->where('id', '=', $result['source_id'])->value('offline_id');
                    break;
                default:
                    $data['factor_name'] = '';
                    $data['warning_name'] = '';
            }
            $data['conversation_content'] = $data['conversation_content'] ? json_decode($data['conversation_content'], true) : [];
            $data['evaluate_content'] = $data['evaluate_content'] ? json_decode($data['evaluate_content'], true) : [];
            $data['event_content'] = $data['event_content'] ? json_decode($data['event_content'], true) : [];
            $data['up_content'] = $data['up_content'] ? json_decode($data['up_content'], true) : [];
            $data['follow_up_content'] = $data['follow_up_content'] ? json_decode($data['follow_up_content'], true) : [];
            $data['operator_role_name'] = $data['operator_role_name'] ?? '超级管理员';
            $data['follow_operator_role_name'] = $data['follow_operator_role_name'] ?? '超级管理员';
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 添加预检员
     * @return bool|string
     */
    public function AddInterfereAdmin(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'uid|管理员' => 'require|integer|max:11',
            ]);
            $InterfereAdminModel = new InterfereAdminModel();
            $is_exist = $InterfereAdminModel->where('uid', '=', $param['uid'])->value('id');
            if ($is_exist) throw new \Exception('不可重复添加');
            $param['time'] = time();
            $param['unit_id'] = $this->userData['unit_id'];
            $InterfereAdminModel->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 删除预检员
     * @return bool|string
     */
    public function InterfereAdminDel(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereAdminModel = new InterfereAdminModel();
            $info = $InterfereAdminModel->find($id);
            if (!$info) throw new \Exception('参数错误！');
            $info->is_del = 1;
            $info->del_time = time();
            $info->save();
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 预检员列表
     * @return bool|string
     */
    public function InterfereAdminList(): bool|string
    {
        try {
            $param = $this->request->param();
            $InterfereAdminModel = new InterfereAdminModel();
            $data = $InterfereAdminModel->where('unit_id', '=', $this->userData['unit_id'])->with([
                'user' => function ($query) {
                    $query->with([
                        'DepartmentAdmins' => function ($query) {
                            $query->with(['Department']);
                        }
                    ]);
                }
            ])->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$item) {
                if (!empty($item['DepartmentAdmins'])) {
                    $role_mode = array_column($item['DepartmentAdmins'], 'department_level');
                    sort($role_mode);
                    $item['role_mode'] = $role_mode[0] . '级管理员';
                } else {
                    $item['role_mode'] = '0级管理员';
                }
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 预检员选择器
     * @return bool|string
     */
    public function AdminList(): bool|string
    {
        try {
            $UserModel = new UserModel();
            $list = $UserModel->field('id,name')->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['role_mode', '=', 1]
            ])->select()->toArray();
            $InterfereAdminModel = new InterfereAdminModel();
            $uids = $InterfereAdminModel->where('unit_id', '=', $this->userData['unit_id'])->column('uid');
            $data = [];
            if (count($uids) > 0) {
                foreach ($list as $item) {
                    if (!in_array($item['id'], $uids)) {
                        $data[] = $item;
                    }
                }
            } else {
                $data = $list;
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 异常行为表现选择器
     * @return bool|string
     */
    public function CrisisBehaviorSelector(): bool|string
    {
        try {
            $where = [];
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $where[] = ['status', '=', 1];
            $CrisisBehaviorModel = new CrisisBehaviorModel();
            $data = $CrisisBehaviorModel->where($where)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 风险等级评估选择器
     * @return bool|string
     */
    public function CrisisLevelSelector(): bool|string
    {
        try {
            $where = [];
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $where[] = ['status', '=', 1];
            $CrisisLevelModel = new CrisisLevelModel();
            $data = $CrisisLevelModel->where($where)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 疑似问题类型选择器
     * @return bool|string
     */
    public function CrisisTypeSelector(): bool|string
    {
        try {
            $where = [];
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $where[] = ['status', '=', 1];
            $CrisisTypeModel = new CrisisTypeModel();
            $data = $CrisisTypeModel->where($where)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 部门的用户选择器
     * @return bool|string
     */
    public function DepartmentUserSelector(): bool|string
    {
        try {
            $department_id = $this->request->param('department_id');
            $UserModel = new UserModel();
            $data = $UserModel->where('department_id', '=', $department_id)->field('id,name')->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 从关注人群移入干预人群
     * @return bool|string
     */
    public function FollowInterfere(): bool|string
    {
        $FollowUserModel = new FollowUserModel();
        $FollowUserModel->startTrans();
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'channel_id|关注ID' => 'require|integer|max:11',
                'type_id|问题类型' => 'require|integer|max:11',
                'level_id|风险等级评估' => 'require|integer|max:11',
                'describe|移入说明' => 'require',
            ]);
            $FollowInfo = $FollowUserModel->find($param['channel_id']);
            if (!$FollowInfo) throw new \Exception('参数错误！');
            $behavior_id = $param['behavior_id'] ?? [];
            unset($param['behavior_id']);
            $param['unit_id'] = $this->userData['unit_id'];
            $param['applicant_uid'] = $this->userData['id'];
            $param['applicant_time'] = time();
            $param['uid'] = $FollowInfo['uid'];
            $param['channel'] = $FollowInfo->channel;
            $param['channel_id'] = $FollowInfo->channel_id;
            $param['follow_id'] = $FollowInfo->id;
            $param['time'] = time();
            $UnitModel = new UnitModel();
            $UnitInfo = $UnitModel->find($this->userData['unit_id']);
            if ($UnitInfo['interfere_admin_status'] == 1) {
                $InterfereAdminModel = new InterfereAdminModel();
                $uid = $InterfereAdminModel->where('unit_id', '=', $this->userData['unit_id'])->orderRand()->value('uid');
                if ($uid) {
                    $param['operator_uid'] = $uid;
                } else {
                    $UserModel = new UserModel();
                    $param['operator_uid'] = $UserModel->where([
                        ['unit_id', '=', $this->userData['unit_id']],
                        ['role_mode', '=', 0]
                    ])->value('id');
                }
            } else {
                $UserModel = new UserModel();
                $param['operator_uid'] = $UserModel->where([
                    ['unit_id', '=', $this->userData['unit_id']],
                    ['role_mode', '=', 0]
                ])->value('id');
            }
            $InterfereUserModel = new InterfereUserModel();
            $interfere_id = $InterfereUserModel->insertGetId($param);
            $InterfereBehaviorModel = new InterfereBehaviorModel();
            $InterfereBehaviorModel->where('interfere_id', '=', $interfere_id)->update([
                'is_del' => 1,
                'del_time' => time()
            ]);
            if (!empty($behavior_id)) {
                $BehaviorData = [];
                foreach ($behavior_id as $v) {
                    $BehaviorData[] = [
                        'interfere_id' => $interfere_id,
                        'behavior_id' => $v,
                        'time' => time()
                    ];
                }
                $InterfereBehaviorModel->insertAll($BehaviorData);
            }
            $FollowUserModel->commit();
            return success();
        } catch (\Exception $e) {
            $FollowUserModel->rollback();
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预人群编辑
     * @return bool|string
     */
    public function InterfereUserUpdate(): bool|string
    {
        $InterfereUserModel = new InterfereUserModel();
        $InterfereUserModel->startTrans();
        try {
            //干预
            $param = $this->request->param();
            $info = $InterfereUserModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if (!empty($param['up_content'])) {
                $param['up_num'] = count($param['up_content']);
                $param['up_content'] = json_encode($param['up_content'], true);
            } else {
                $param['up_num'] = 0;
                $param['up_content'] = '';
            }
            if (!empty($param['conversation_content'])) {
                $param['conversation_num'] = count($param['conversation_content']);
                $param['conversation_content'] = json_encode($param['conversation_content'], true);
            } else {
                $param['conversation_num'] = 0;
                $param['conversation_content'] = '';
            }
            if (!empty($param['evaluate_content'])) {
                $param['evaluate_num'] = count($param['evaluate_content']);
                $param['evaluate_content'] = json_encode($param['evaluate_content'], true);
            } else {
                $param['evaluate_num'] = 0;
                $param['evaluate_content'] = '';
            }
            if (!empty($param['event_content'])) {
                $param['event_num'] = count($param['event_content']);
                $param['event_content'] = json_encode($param['event_content'], true);
            } else {
                $param['event_num'] = 0;
                $param['event_content'] = '';
            }
            if ($param['up_num'] != $info->up_num) {
                //消息记录盒子
                $CrisisLogModel = new CrisisLogModel();
                $CrisisLogModel->insert([
                    'unit_id' => $this->userData['unit_id'],
                    'uid' => $info->uid,
                    'type' => 3,
                    'source_type' => 2,
                    'behavior' => '干预人群跟进',
                    'time' => time()
                ]);
            }
            $behavior_id = $param['behavior_id'] ?? [];
            unset($param['behavior_id']);
            $info->save($param);
            //更新行为标签
            $InterfereBehaviorModel = new InterfereBehaviorModel();
            $InterfereBehaviorModel->where('interfere_id', '=', $param['id'])->update([
                'is_del' => 1,
                'del_time' => time()
            ]);
            if (!empty($behavior_id)) {
                $BehaviorData = [];
                foreach ($behavior_id as $v) {
                    $BehaviorData[] = [
                        'interfere_id' => $param['id'],
                        'behavior_id' => $v,
                        'time' => time()
                    ];
                }
                $InterfereBehaviorModel->insertAll($BehaviorData);
            }
            $InterfereUserModel->commit();
            return success();
        } catch (\Exception $e) {
            $InterfereUserModel->rollback();
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 干预完成档案列表
     * @return bool|string
     */
    public function InterfereUserArchivesList(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            $UserModel = new NotDelUserModel();
            if (!empty($param['department_id']) && !empty($param['name'])) {
                $uids = $UserModel->where([
                    ['name|username', 'like', '%' . $param['name'] . '%'],
                    ['department_id', '=', $param['department_id']],
                ])->column('id');
                if (!empty($uids)) {
                    $where[] = ['uid', 'in', $uids];
                } else {
                    $where[] = ['id', '=', '-1'];
                }
            } else {
                if (!empty($param['name'])) {
                    $uids = $UserModel->where([
                        ['unit_id', '=', $this->userData['unit_id']],
                        ['name|username', 'like', '%' . $param['name'] . '%']
                    ])->column('id');
                    if (!empty($uids)) {
                        $where[] = ['uid', 'in', $uids];
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                }
                if (!empty($param['department_id'])) {
                    $uids = $UserModel->where('department_id', '=', $param['department_id'])->column('id');
                    if (!empty($uids)) {
                        $where[] = ['uid', 'in', $uids];
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                }
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['move_time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['move_time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['move_time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['type_id'])) {
                $where[] = ['type_id', '=', $param['type_id']];
            }
            if (!empty($param['level_id'])) {
                $where[] = ['level_id', '=', $param['level_id']];
            }
            if (!empty($param['behavior_id'])) {
                $InterfereBehaviorModel = new InterfereBehaviorModel();
                $ids = $InterfereBehaviorModel->where('behavior_id', '=', $param['behavior_id'])->column('interfere_id');
                if (!empty($ids)) {
                    $where[] = ['id', 'in', $ids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['channel'])) {
                $where[] = ['channel', '=', $param['channel']];
            }
            if (!empty($param['uid'])) {
                $where[] = ['uid', '=', $param['uid']];
            }
            if (!empty($param['id'])) {
                $where[] = ['id', 'in', $param['id']];
            }
            switch ($this->userData['role_mode']) {
                case 1:
                    $DepartmentAdminModel = new DepartmentAdminModel();
                    $department_ids = $DepartmentAdminModel->where('uid', '=', $this->userData['id'])->column('department_id');
                    if (!empty($department_ids)) {
                        $uid = $UserModel->where('department_id', 'in', $department_ids)->column('id');
                        if (!empty($uid)) {
                            $where[] = ['uid', 'in', $uid];
                        } else {
                            $where[] = ['id', '=', '-1'];
                        }
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                    break;
                case 2:
                    $ConsultOfflineModel = new ConsultOfflineModel();
                    $ConsultOnlineModel = new ConsultOnlineModel();
                    $uid = $ConsultOnlineModel->where('consult_id', '=', $this->userData['id'])->distinct(true)->column('uid');
                    $uid2 = $ConsultOfflineModel->where('consult_id', '=', $this->userData['id'])->distinct(true)->column('uid');
                    $uid3 = array_merge($uid, $uid2);
                    if (!empty($uid3)) {
                        $uid3 = array_unique($uid3);
                        $where[] = ['uid', 'in', $uid3];
                    } else {
                        $where[] = ['id', '=', '-1'];
                    }
                    break;
                case 3:
                    $where[] = ['id', '=', -1];
            }
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $where[] = ['status', '=', 1];
            $InterfereUserModel = new InterfereUserModel();
            $query = $InterfereUserModel->where($where);
            $data = $query->with([
                'user' => function ($query) {
                    $query->with([
                        'department' => function ($query) {
                            $query->with([
                                'departmentAdmins' => function ($query) {
                                    $query->with(['userName']);
                                }
                            ]);
                        },
                        'unitName'
                    ]);
                },
                'InterfereBehavior' => function ($query) {
                    $query->with(['CrisisBehavior']);
                },
                'OperatorUser' => function ($query) {
                    $query->with(['roleName']);
                }
            ])->order('status_time desc')->paginate($param['pageSize'])->toArray();
            if (!empty($data['data'])) {
                $departmentIds = array_column($data['data'], 'department_id');
                $UserService = new UserService($this->app);
                $NotDelDepartmentAll = $UserService->getDepartmentAffiliationAll2($this->userData['unit_id'], $departmentIds);
                foreach ($data['data'] as &$item) {
                    //部门归属
                    $item['department_name'] = $NotDelDepartmentAll['department_data'][$item['department_id']]['all_name'] ?? '';
                    $item['ascription_name'] = $NotDelDepartmentAll['department_data'][$item['department_id']]['ascription_name'] ?? '';
                    $item['operator_role_name'] = $item['operator_role_name'] ?? "超级管理员";
                }
            }
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 更新干预检测员状态
     * @return bool|string
     */
    public function InterfereAdminStatus(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'interfere_admin_status|干预检测员状态' => 'require|in:0,1',
            ]);
            $UnitModel = new UnitModel();
            $info = $UnitModel->find($this->userData['unit_id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save([
                'interfere_admin_status' => $param['interfere_admin_status']
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 批量导出关注人群
     * @return bool|string
     */
    public function FollowUserExport(): bool|string
    {
        try {
            $id = $this->request->param('id');
            if (!is_array($id)) throw new \Exception('参数错误！');
            $InterfereService = new InterfereExportService($this->app);
            return success($InterfereService->FollowUserExport($id, $this->userData['unit_id']));
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 批量导出干预人群
     * @return bool|string
     */
    public function InterfereUserExport(): bool|string
    {
        try {
            $id = $this->request->param('id');
            if (!is_array($id)) throw new \Exception('参数错误！');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->InterfereUserExport($id, $this->userData['unit_id']);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 批量导出干预完成档案
     * @return bool|string
     */
    public function InterfereUserArchivesExport(): bool|string
    {
        try {
            $id = $this->request->param('id');
            if (!is_array($id)) throw new \Exception('参数错误！');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->InterfereUserArchivesExport($id, $this->userData['unit_id']);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 导入关注人群
     * @return bool|string
     */
    public function FollowUserImport(): bool|string
    {
        try {
            $file = $this->request->param('file');
            $ConfigModel = new ConfigModel();
            $config = $ConfigModel->where([
                'unit_id' => $this->userData['unit_id'],
                'key' => 'department_management'
            ])->value('value') ?? '';
            $config = json_decode($config, true);
            $InterfereImportService = new InterfereImportService($this->app);
            if ($config['type'] == 1) {
                if (!$InterfereImportService->FollowUser($file, $this->userData)) throw new \Exception($InterfereImportService->message);
            } else {
                if (!$InterfereImportService->SchoolFollowUser($file, $this->userData)) throw new \Exception($InterfereImportService->message);
            }
            return success([], '导入成功！');
        } catch (\Exception $e) {
            // 上传单文件
            $upload = new \app\common\Upload();
            // 设置上传类型和文件大小
            $upload = $upload->defaultConfig();
            $ext = '.txt';
            $save_name = '/upload/' . date('Ym') . '/' . date('d') . '_' . time() . uniqid();
            $url = $upload->upload($e->getMessage(), 'binary', $ext, $save_name);
            return error('导入失败！', $e->getCode(), [
                'url' => $url
            ]);
//            return error($e->getMessage(), $e->getCode(),[
//                'line' => $e->getLine(),
//                'file' => $e->getFile(),
//            ]);
        }
    }

    /**
     * 导入干预人群
     * @return bool|string
     */
    public function InterfereUserImport(): bool|string
    {
        try {
            $file = $this->request->param('file');
            $ConfigModel = new ConfigModel();
            $config = $ConfigModel->where([
                'unit_id' => $this->userData['unit_id'],
                'key' => 'department_management'
            ])->value('value') ?? '';
            $config = json_decode($config, true);
            $InterfereImportService = new InterfereImportService($this->app);
            if ($config['type'] == 1) {
                if (!$InterfereImportService->InterfereUser($file, $this->userData)) throw new \Exception($InterfereImportService->message);
            } else {
                if (!$InterfereImportService->SchoolInterfereUser($file, $this->userData)) throw new \Exception($InterfereImportService->message);
            }
            return success([], '导入成功！');
        } catch (\Exception $e) {
            // 上传单文件
            $upload = new \app\common\Upload();
            // 设置上传类型和文件大小
            $upload = $upload->defaultConfig();
            $ext = '.txt';
            $save_name = '/upload/' . date('Ym') . '/' . date('d') . '_' . time() . uniqid();
            $url = $upload->upload($e->getMessage(), 'binary', $ext, $save_name);
            return error('导入失败！', $e->getCode(), [
                'url' => $url
            ]);
//            return error($e->getMessage(), $e->getCode(),[
//                'line' => $e->getLine(),
//                'file' => $e->getFile(),
//            ]);
        }
    }

    /**
     * 下载模板
     * @return false|string
     */
    public function downloadTemplate(): bool|string
    {
        $ConfigModel = new ConfigModel();
        $config = $ConfigModel->where([
            'unit_id' => $this->userData['unit_id'],
            'key' => 'department_management'
        ])->value('value') ?? '';
        $config = json_decode($config, true);
        $type = $this->request->param('type');
        switch ($type) {
            case 1:
                if ($config['type'] == 1) {
                    $url = '/template/follow_user.xlsx';
                } else {
                    $url = '/template/school_follow_user.xlsx';
                }
                break;
            case 2:
                if ($config['type'] == 1) {
                    $url = '/template/interfere_user.xlsx';
                } else {
                    $url = '/template/school_interfere_user.xlsx';
                }
                break;
            default:
                return error('参数错误！');
        }
        return success(['url' => $url]);
    }

    /**
     * 危机干预月报导出
     * @return bool|string
     */
    public function MonthlyReport(): bool|string
    {
        try {
            $param = $this->request->param();
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->MonthlyReport($param, [
                'unit_id' => $this->userData['unit_id'],
            ]);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 用户危机干预档案归档情况导出
     * @return bool|string
     */
    public function ArchivingStatus(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->ArchivingStatus($id, $this->userData['unit_id']);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * word转PDF
     * @return bool|string
     */
    public function WordToPdf(): bool|string
    {
        try {
            $file = $this->request->file('file');
            if (empty($file)) throw new \Exception('请选择文件');
            // 上传操作
            // 上传单文件
            $path = '/storage/' . Filesystem::disk('public')->putFile('file', $file);
            WordToPdf('.' . $path);
            $path = str_replace('.docx', '', $path);
            $path = str_replace('.doc', '', $path);
            $path = oss('./' . $path . '.pdf', '.pdf');
            return success($path);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 关注期情况记录表导出
     * @return bool|string
     */
    public function FollowArchive(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->FollowArchive($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }


    /**
     * 干预期情况记录表导出
     * @return bool|string
     */
    public function InterfereArchive(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->InterfereArchive($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 消息记录盒子
     * @return bool|string
     */
    public function CrisisLogList(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['name'])) {
                $UserModel = new NotDelUserModel();
                $uid = $UserModel->where([
                    ['unit_id', '=', $this->userData['unit_id']],
                    ['name', 'like', '%' . $param['name'] . '%']
                ])->column('id');
                if (!empty($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['type'])) {
                $where[] = ['type', '=', $param['type']];
            }
            if (!empty($param['source_type'])) {
                $where[] = ['source_type', '=', $param['source_type']];
            }
            if (isset($param['is_remind']) && strlen($param['is_remind']) > 0) {
                if ($param['is_remind'] == 1) {
                    $where[] = ['remind_time', '>', 0];
                } else {
                    $where[] = ['remind_time', '=', 0];
                }
            }
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $CrisisLogModel = new CrisisLogModel();
            $query = $CrisisLogModel->where($where);
            $data = $query->with(['userName'])->order('id desc')->paginate($param['pageSize'])->toArray();
            $UserService = new UserService($this->app);
            foreach ($data['data'] as &$item) {
                if ($item['uid'] == 0) {
                    $item['user_name'] = '用户待审核';
                    $item['user_avatar'] = $UserService->getDefaultAvatar('male');
                }
            }
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 消息记录盒子-取消提醒
     * @return bool|string
     */
    public function CrisisLogCancel(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisLogModel = new CrisisLogModel();
            $info = $CrisisLogModel->find($id);
            if (!$info) throw new \Exception('参数错误！');
            $info->remind_time = 0;
            $info->save();
            return success([], '取消成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 消息记录盒子-设置提醒
     * @return bool|string
     */
    public function CrisisLogSetRemind(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|记录ID' => 'require|integer|max:11',
                'day|提醒时间' => 'require',
            ]);
            $CrisisLogModel = new CrisisLogModel();
            $info = $CrisisLogModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $time = time() + ($param['day'] * 24 * 60 * 60);
            $info->remind_time = $time;
            $info->remind_uid = $this->userData['id'];
            $info->save();
            return success([], '设置成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 消息记录盒子-删除
     * @return bool|string
     */
    public function CrisisLogDel(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CrisisLogModel = new CrisisLogModel();
            if (is_array($id)) {
                $CrisisLogModel->where('id', 'in', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            } else {
                $CrisisLogModel->where('id', '=', $id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }
            return success([], '删除成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预完成记录-线上咨询记录导出
     * @return bool|string
     */
    public function ArchivingConsultOnline(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->ArchivingConsultOnline($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预完成记录-线下预约记录导出
     * @return bool|string
     */
    public function ArchivingConsultOffline(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->ArchivingConsultOffline($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 上报
     * @return bool|string
     */
    public function report(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'name|名称' => 'require',
                'type|类型' => 'require',
                'start_time|开始时间' => 'require',
                'end_time|结束时间' => 'require',
                'trend|危机人员心理动向' => 'require',
                'health_advice|心理健康建议' => 'require',
                'experience|特殊经历及感悟' => 'require',
                'suggestion|其他问题及建议' => 'require',
                'self_evaluation|自我工作评价' => 'require',
            ]);
            $InterfereService = new InterfereExportService($this->app);
            $param['url'] = $InterfereService->MonthlyReport2($param, [
                'unit_id' => $this->userData['unit_id'],
            ]);
            $param['channel'] = implode(',', $param['channel'] ?? []);
            $param['type_id'] = implode(',', $param['type_id'] ?? []);
            $param['time'] = time();
            $param['uid'] = $this->userData['id'];
            $param['unit_id'] = $this->userData['unit_id'];
            $InterfereReportModel = new InterfereReportModel();
            $InterfereReportModel->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 我的上报记录
     * @return bool|string
     */
    public function myReport(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'pageSize|每页显示数量' => 'require'
            ]);
            $where = [];
            if (!empty($param['name'])) {
                $where[] = ['name', 'like', '%' . $param['name'] . '%'];
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['type'])) {
                $where[] = ['type', '=', $param['type']];
            }
            $where[] = ['uid', '=', $this->userData['id']];
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $InterfereReportModel = new InterfereReportModel();
            $query = $InterfereReportModel->where($where);
            $data = $query->with([
                'user'
            ])->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 删除上报记录
     * @return bool|string
     */
    public function deleteReport(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereReportModel = new InterfereReportModel();
            if (is_array($param['id'])) {
                $InterfereReportModel->where('id', 'in', $param['id'])->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            } else {
                $InterfereReportModel->where('id', '=', $param['id'])->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 我收到的月报
     * @return bool|string
     */
    public function adminReport(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'pageSize|每页显示数量' => 'require'
            ]);
            $where = [];
            if (!empty($param['name'])) {
                $where[] = ['name', 'like', '%' . $param['name'] . '%'];
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['type'])) {
                $where[] = ['type', '=', $param['type']];
            }
            if (!empty($param['uid'])) {
                $where[] = ['uid', '=', $param['uid']];
            }
            if ($this->userData['role_id'] > 0) {
                $DepartmentAdminModel = new DepartmentAdminModel();
                $department_id = $DepartmentAdminModel->where('uid', '=', $this->userData['id'])->column('department_id');
                if (!empty($department_id)) {
                    $UserService = new UserService($this->app);
                    $allLowerId = $UserService->getDepartmentAllLowerLevelIds($this->userData['unit_id'], $department_id);
                    $uid = $DepartmentAdminModel->where('department_id', $allLowerId)->column('uid');
                    if (!empty($uid)) {
                        $where[] = ['uid', 'in', $uid];
                    } else {
                        $where[] = ['id', '=', -1];
                    }
                }
            }
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $InterfereReportModel = new InterfereReportModel();
            $query = $InterfereReportModel->where($where);
            $data = $query->with([
                'user'
            ])->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 上报人选择器
     * @return bool|string
     */
    public function reportUidSelector(): bool|string
    {
        try {
            $where = [];
            if ($this->userData['role_id'] > 0) {
                $DepartmentAdminModel = new DepartmentAdminModel();
                $department_id = $DepartmentAdminModel->where('uid', '=', $this->userData['id'])->column('department_id');
                if (!empty($department_id)) {
                    $UserService = new UserService($this->app);
                    $allLowerId = $UserService->getDepartmentAllLowerLevelIds($this->userData['unit_id'], $department_id);
                    $uid = $DepartmentAdminModel->where('department_id', $allLowerId)->column('uid');
                    if (!empty($uid)) {
                        $where[] = ['uid', 'in', $uid];
                    } else {
                        $where[] = ['id', '=', -1];
                    }
                }
            }
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $InterfereReportModel = new InterfereReportModel();
            $uid = $InterfereReportModel->where($where)->column('uid');
            $UserModel = new UserModel();
            $data = $UserModel->field('id,name')->where('id', 'in', $uid)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 导出上报记录
     * @return bool|string
     */
    public function exportReport(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereReportModel = new InterfereReportModel();
            $url = $InterfereReportModel->where('id', 'in', $param['id'])->column('url');
            if (empty($url)) throw new \Exception('暂无可导出的信息！');
            $ExportService = new ExportService($this->app);
            switch (env('UPLOAD_TYPE')) {
                case "local":
                    if (!$ExportService->pack($url)) throw new \Exception($ExportService->message);
                    break;
                case "oss":
                    if (!$ExportService->streamCompress($url)) throw new \Exception($ExportService->message);
                    break;
                default:
                    throw new \Exception('系统配置错误！');
            }
            return success($ExportService->data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 谈心谈话记录
     * @return bool|string
     */
    public function conversationLog(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'uid|用户' => 'require'
            ]);
            $where[] = ['uid', '=', $param['uid']];
            $InterfereUserModel = new InterfereUserModel();
            $list = $InterfereUserModel->where($where)->column('conversation_content');
            $data = [];
            foreach ($list as $item) {
                if (!empty($item)) {
                    $data = json_decode($item, true);
                }
            }
            $field = [];
            foreach ($data as $key => $row) {
                $field[$key] = $row['time'];
            }
            array_multisort($field, SORT_DESC, $data); // 假设按’field’字段升序排序
            if (!empty($data)) {
                $time = $data[0]['time'];
            } else {
                $time = 0;
            }
            return success([
                'time' => $time,
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 智能预检详情
     * @return bool|string
     */
    public function intelligentInfo(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $info = $InterfereUserModel->with([
                'OperatorUser' => function ($query) {
                    $query->with(['roleName']);
                },
                'user' => function ($query) {
                    $query->with([
                        'role_name',
                        'unitName',
                        'department' => function ($query) {
                            $query->with([
                                'departmentAdmins' => function ($query) {
                                    $query->with(['userName']);
                                }
                            ]);
                        }
                    ]);
                },
                'InterfereBehavior' => function ($query) {
                    $query->with(['CrisisBehavior']);
                },
                'FollowUser' => function ($query) {
                    $query->with([
                        'operator' => function ($query) {
                            $query->with(['roleName']);
                        }
                    ]);
                },
                'opinionUser',
                'opinionUser2',
                'opinionUser3',
                'evaluateUser',
                'moveUser'
            ])->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $data = $info->toArray();
            $data['up_content'] = $data['up_content'] ? json_decode($data['up_content'], true) : [];
            $data['follow_up_content'] = $data['follow_up_content'] ? json_decode($data['follow_up_content'], true) : [];
            $data['operator_role_name'] = $data['operator_role_name'] ?? '超级管理员';
            $data['follow_operator_role_name'] = $data['follow_operator_role_name'] ?? '超级管理员';
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 智能预检列表
     * @return bool|string
     */
    public function intelligent(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'pageSize|每页显示数量' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $UserModel = new UserModel();
            $where = [];
            if (!empty($param['name'])) {
                $uid = $UserModel->where('name', 'like', '%' . $param['name'] . '%')->column('id');
                if (!empty($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['move_time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['move_time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['move_time', '<=', $param['end_time']];
                }
            }
            if (isset($param['move_status']) && strlen($param['move_status']) > 1) {
                $where[] = ['move_status', '=', $param['move_status']];
            }
            if (isset($param['status']) && strlen($param['status']) > 1) {
                if ($param['status'] == 1) {
                    $where[] = ['move_status', 'in', [1, 2]];
                } else {
                    $where[] = ['move_status', '=', 0];
                }
            }
            if (!empty($param['level'])) {
                $where[] = ['level', '=', $param['level']];
            }
            if (!empty($param['level_id'])) {
                $where[] = ['level_id', '=', $param['level_id']];
            }
            if (!empty($param['operator_uid'])) {
                $where[] = ['operator_uid', '=', $param['operator_uid']];
            }
            $where[] = ['is_del2', '=', 0];
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $query = $InterfereUserModel->where($where);
            $data = $query->with([
                'user',
                'OperatorUser'
            ])->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 多方评估列表
     * @return bool|string
     */
    public function multiParty(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'pageSize|每页显示数量' => 'require'
            ]);

            $InterfereUserModel = new InterfereUserModel();
            $UserModel = new UserModel();
            $where = [];
            if (!empty($param['name'])) {
                $uid = $UserModel->where('name', 'like', '%' . $param['name'] . '%')->column('id');
                if (!empty($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['move_time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['move_time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['move_time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['level'])) {
                $where[] = ['level', '=', $param['level']];
            }
            if (!empty($param['level_id'])) {
                $where[] = ['level_id', '=', $param['level_id']];
            }
            if (!empty($param['move_uid'])) {
                $where[] = ['move_uid', '=', $param['move_uid']];
            }
            if (!empty($param['evaluate_status'])) {
                $where[] = ['evaluate_status', '=', $param['evaluate_status']];
            }
            $where[] = ['is_del3', '=', 0];
            $where[] = ['move_status', '=', 1];
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $query = $InterfereUserModel->where($where);
            $data = $query->with([
                'user',
                'moveUser'
            ])->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 实施干预
     * @return bool|string
     */
    public function evaluate(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'pageSize|每页显示数量' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $UserModel = new UserModel();
            $where = [];
            if (!empty($param['name'])) {
                $uid = $UserModel->where('name|username', 'like', '%' . $param['name'] . '%')->column('id');
                if (!empty($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['evaluate_time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['evaluate_time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['evaluate_time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['priority'])) {
                $where[] = ['priority', '=', $param['priority']];
            }
            if (!empty($param['level'])) {
                $where[] = ['level', '=', $param['level']];
            }
            if (!empty($param['level_id'])) {
                $where[] = ['level_id', '=', $param['level_id']];
            }
            if (!empty($param['evaluate_uid'])) {
                $where[] = ['evaluate_uid', '=', $param['evaluate_uid']];
            }
            $where[] = ['is_del4', '=', 0];
            $where[] = ['evaluate_status', '=', 2];
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $query = $InterfereUserModel->where($where);
            $data = $query->with([
                'evaluateUser',
                'user',
                'InterfereBehavior' => function ($query) {
                    $query->with(['CrisisBehavior']);
                },
            ])->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 智能预检-更新
     * @return bool|string
     */
    public function intelligentUpdate(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $info = $InterfereUserModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if (!empty($param['move_status'])) {
                $param['status_time'] = time();
                $param['move_uid'] = $this->userData['id'];
            }
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 多方评估-更新
     * @return bool|string
     */
    public function intelligentUpdate2(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $info = $InterfereUserModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if (!empty($param['evaluate_status'])) {
                $param['evaluate_time'] = time();
                $param['evaluate_uid'] = $this->userData['id'];
            }
            if (!empty($param['opinion']) || !empty($param['opinion_describe']) || !empty($param['opinion_sgin'])) {
                $RoleModel = new RoleModel();
                $role_name = $RoleModel->where('id', '=', $this->userData['role_id'])->value('name');
                if ($role_name != "校领导") throw new \Exception('校领导意见只有校领导可填写！');
                $param['opinion_uid'] = $this->userData['id'];
                $param['opinion_time'] = time();
            }
            if (!empty($param['opinion2']) || !empty($param['opinion_describe2']) || !empty($param['opinion_sgin2'])) {
                if ($this->userData['role_id'] > 0) throw new \Exception('超管意见只有超管可填写！');
                $param['opinion2_uid'] = $this->userData['id'];
                $param['opinion2_time'] = time();
            }
            if (!empty($param['opinion3']) || !empty($param['opinion_describe3']) || !empty($param['opinion_sgin3'])) {
                if ($this->userData['role_mode'] != 2) throw new \Exception('咨询师意见只有咨询师可填写！');
                $param['opinion3_uid'] = $this->userData['id'];
                $param['opinion3_time'] = time();
            }
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 实施干预-更新
     * @return bool|string
     */
    public function intelligentUpdate3(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $info = $InterfereUserModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if (!empty($param['up_content'])) {
                $param['up_num'] = count($param['up_content']);
                $param['up_content'] = json_encode($param['up_content'], true);
            } else {
                $param['up_num'] = 0;
                $param['up_content'] = '';
            }
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 结束干预
     * @return bool|string
     */
    public function endInterfere(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require',
                'result_status|干预结果' => 'require',
                'result_describe|补充说明' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $info = $InterfereUserModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $param['status'] = 1;
            $param['status_time'] = time();
            $info->inc('status_num')->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    public function del2()
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $InterfereUserModel->where('id', 'in', $param['id'])->update([
                'is_del2' => 1
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    public function del3()
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $InterfereUserModel->where('id', 'in', $param['id'])->update([
                'is_del3' => 1
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    public function del4()
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $InterfereUserModel->where('id', 'in', $param['id'])->update([
                'is_del4' => 1
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 智能预检-预检负责人
     * @return bool|string
     */
    public function selector(): bool|string
    {
        try {
            $InterfereUserModel = new InterfereUserModel();
            $UserModel = new UserModel();
            $uid = $InterfereUserModel->where('unit_id', '=', $this->userData['unit_id'])->distinct(true)->column('operator_uid');
            $data = $UserModel->field('id,name')->where('id', 'in', $uid)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 智能预检-检测是否可以删除
     * @return bool|string
     */
    public function is_del(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $move_status = $InterfereUserModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['move_status', '=', 0],
                ['id', 'in', $param['id']]
            ])->value('id');
            $status = $move_status ? 1 : 0;
            return success($status);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 智能预检-移入评估人
     * @return bool|string
     */
    public function selector2(): bool|string
    {
        try {
            $InterfereUserModel = new InterfereUserModel();
            $UserModel = new UserModel();
            $uid = $InterfereUserModel->where('unit_id', '=', $this->userData['unit_id'])->distinct(true)->column('move_uid');
            $data = $UserModel->field('id,name')->where('id', 'in', $uid)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 实施干预-干预负责人
     * @return bool|string
     */
    public function selector3(): bool|string
    {
        try {
            $InterfereUserModel = new InterfereUserModel();
            $UserModel = new UserModel();
            $uid = $InterfereUserModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['evaluate_status', '=', 2]
            ])->distinct(true)->column('evaluate_uid');
            $data = $UserModel->field('id,name')->where('id', 'in', $uid)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 实施干预-检测是否可以删除
     * @return bool|string
     */
    public function is_del2(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereUserModel = new InterfereUserModel();
            $move_status = $InterfereUserModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['id', 'in', $param['id']],
                ['evaluate_status', '=', 2]
            ])->value('id');
            $status = $move_status ? 1 : 0;
            return success($status);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 实施干预-导出
     * @return bool|string
     */
    public function export(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereExportService = new InterfereExportService($this->app);
            return success($InterfereExportService->InterfereUserExport2($param['id'], $this->userData['unit_id']));
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 实施干预-导入
     * @return bool|string
     */
    public function import()
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'file|文件' => 'require'
            ]);
            $ConfigModel = new ConfigModel();
            $config = $ConfigModel->where([
                'unit_id' => $this->userData['unit_id'],
                'key' => 'department_management'
            ])->value('value') ?? '';
            $config = json_decode($config, true);
            $InterfereImportService = new InterfereImportService($this->app);
            if ($config['type'] == 1) {
                if (!$InterfereImportService->FollowUser($param['file'], $this->userData)) throw new \Exception($InterfereImportService->message);
            } else {
                if (!$InterfereImportService->SchoolFollowUser($param['file'], $this->userData)) throw new \Exception($InterfereImportService->message);
            }
            return success([], '导入成功！');
        } catch (\Exception $e) {
            // 上传单文件
            $upload = new \app\common\Upload();
            // 设置上传类型和文件大小
            $upload = $upload->defaultConfig();
            $ext = '.txt';
            $save_name = '/upload/' . date('Ym') . '/' . date('d') . '_' . time() . uniqid();
            $url = $upload->upload($e->getMessage(), 'binary', $ext, $save_name);
            return error('导入失败！', $e->getCode(), [
                'url' => $url
            ]);
//            return error($e->getMessage(), $e->getCode(),[
//                'line' => $e->getLine(),
//                'file' => $e->getFile(),
//            ]);
        }
    }

    /**
     * 全部导出（高校）
     * @return bool|string
     */
    public function allExport(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require'
            ]);
            $InterfereService = new InterfereExportService($this->app);
            $data = [
                [
                    'name' => '线上咨询',
                    'value' => $InterfereService->ArchivingConsultOnline($param['id'])
                ], [
                    'name' => '线下预约',
                    'value' => $InterfereService->ArchivingConsultOffline($param['id'])
                ], [
                    'name' => '干预期情况',
                    'value' => $InterfereService->InterfereArchive($param['id'])
                ], [
                    'name' => '关注期情况',
                    'value' => $InterfereService->FollowArchive($param['id'])
                ], [
                    'name' => '干预档案',
                    'value' => $InterfereService->ArchivingStatus($param['id'], $this->userData['unit_id'])
                ], [
                    'name' => '多方评估',
                    'value' => $InterfereService->multiEvaluation($param['id'])
                ], [
                    'name' => '智能预检-基本信息',
                    'value' => $InterfereService->check_basic_information($param['id'], $this->userData['unit_id'])
                ], [
                    'name' => '智能预检-背景资料',
                    'value' => $InterfereService->user_background_information($param['id'])
                ], [
                    'name' => '智能预检-问题访谈',
                    'value' => $InterfereService->question_interview($param['id'])
                ], [
                    'name' => '智能预检-预检结果',
                    'value' => $InterfereService->final_result($param['id'])
                ],
            ];
            $zip = new ZipArchive();
            $ext = '.zip';
            $save_name = './export/pack' . date('Ym') . '/' . date('d') . '_' . time() . uniqid();
            folder_build(dirname($save_name . $ext));
            $zipFileName = $save_name . $ext; // 压缩包文件名
            if ($zip->open($zipFileName, ZipArchive::CREATE) !== TRUE)
                throw new \Exception('无法创建压缩文件');
            switch (env('UPLOAD_TYPE')) {
                case "local":
                    foreach ($data as $item) {
                        if (is_readable($item['value'])) {
                            $zip->addFile($item['value'], $item['name'] . '.docx');
                        }
                    }
                    break;
                case "oss":
                    // 初始化OSS客户端
                    $config = config('app.oss');
                    // 获取AK和SK信息
                    $accessKeyId = $config['access_key_id'];
                    $accessKeySecret = $config['access_key_secret'];
                    $provider = new StaticCredentialsProvider($accessKeyId, $accessKeySecret);
                    $endpoint = $config['upload_url'];
                    $ossConfig = array(
                        "provider" => $provider,
                        "endpoint" => $endpoint,
                        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
                        "region" => "cn-beijing"
                    );
                    $ossClient = new OssClient($ossConfig);
                    foreach ($data as $item) {
                        if (is_readable($item['value'])) {
                            // 流式获取OSS文件内容
                            $stream = $ossClient->getObject(
                                $config['bucket_name'],
                                $item['value']
                            );
                            // 直接写入ZIP不落盘
                            $zip->addFromString($item['name'] . '.docx', $stream);
                        }
                    }
                    break;
                default:
                    throw new \Exception('系统配置错误！');
            }
            $zip->close();
            return success([
                'url' => oss($zipFileName, $ext)
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 多方测评档案导出
     * @return bool|string
     */
    public function multiEvaluation(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->multiEvaluation($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 智能预检-基本信息
     * @return bool|string
     */
    public function check_basic_information(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->check_basic_information($id, $this->userData['unit_id']);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 智能预检-背景资料
     * @return bool|string
     */
    public function user_background_information(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->user_background_information($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 智能预检-问题访谈
     * @return bool|string
     */
    public function question_interview(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->question_interview($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 智能预检-预检结果
     * @return bool|string
     */
    public function final_result(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $InterfereService = new InterfereExportService($this->app);
            $data = $InterfereService->final_result($id);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


}